home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / macros / latex209 / contrib / latexinfo / C / info.c < prev    next >
Text File  |  1992-02-22  |  103KB  |  4,502 lines

  1. /* info -- a stand-alone Info program.
  2.  
  3.    Version 1.40
  4.    24 October 1991
  5.  
  6.    Copyright (C) 1987, 1991 Free Software Foundation, Inc.
  7.  
  8.    This file is part of GNU Info.
  9.  
  10.    GNU Info is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY.  No author or distributor accepts
  12.    responsibility to anyone for the consequences of using it or for
  13.    whether it serves any particular purpose or works at all, unless he
  14.    says so in writing.  Refer to the GNU Emacs General Public License
  15.    for full details.
  16.  
  17.    Everyone is granted permission to copy, modify and redistribute
  18.    GNU Info, but only under the conditions described in the GNU Emacs
  19.    General Public License.   A copy of this license is supposed to
  20.    have been given to you along with GNU Emacs so you can know your
  21.    rights and responsibilities.  It should be in a file named COPYING.
  22.    Among other things, the copyright notice and this notice must be
  23.    preserved on all copies.  */
  24.  
  25. #include <stdio.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <signal.h>
  29. #include <pwd.h>
  30. #include <errno.h>
  31. #if !defined (errno)
  32. extern int errno;
  33. #endif /* !errno */
  34. #include <ctype.h>
  35.  
  36. #include "getopt.h"
  37.  
  38. #if defined (USG)
  39. struct passwd *getpwnam ();
  40. #include <fcntl.h>
  41. #include <termio.h>
  42. #include <string.h>
  43.  
  44. #  if defined (USGr3)
  45. #    include <sys/stream.h>
  46. #    include <sys/ptem.h>
  47. #    undef TIOCGETC
  48. #  endif /* USGr3 */
  49. #define bcopy(source, dest, count) memcpy(dest, source, count)
  50. char *index(s,c) char *s; { char *strchr(); return strchr(s,c); }
  51. char *rindex(s,c) char *s; { char *strrchr(); return strrchr(s,c); }
  52. #else /* !USG */
  53. #include <sys/file.h>
  54. #include <sgtty.h>
  55. #include <strings.h>
  56. #endif /* USG */
  57.  
  58. #if !defined (DEFAULT_INFOPATH)
  59. #define DEFAULT_INFOPATH ".:/usr/gnu/info:/usr/local/emacs/info:/usr/local/lib/emacs/info"
  60. #endif /* DEFAULT_INFOPATH */
  61.  
  62. typedef int boolean;
  63. #define true 1
  64. #define false 0
  65. #define UNIX
  66.  
  67. typedef struct nodeinfo {
  68.   char *filename;
  69.   char *nodename;
  70.   int pagetop;
  71.   int nodetop;
  72.   struct nodeinfo *next;
  73. } NODEINFO;
  74.  
  75. typedef struct indirectinfo {
  76.   char *filename;
  77.   int first_byte;
  78. } INDIRECT_INFO;
  79.  
  80. typedef int Function ();
  81.  
  82. #define PROJECT_NAME "GNU Info"
  83.  
  84. #define barf(msg) fprintf(stderr, "%s\n", msg)
  85.  
  86. /* Some character stuff. */
  87. #define control_character_threshold 0x020 /* smaller than this is control */
  88. #define meta_character_threshold 0x07f    /* larger than this is Meta. */
  89. #define control_character_bit 0x40    /* 0x000000, must be off. */
  90. #define meta_character_bit 0x080/* x0000000, must be on. */
  91.  
  92. #define info_separator_char '\037'
  93. #define start_of_node_string "\037"
  94.  
  95. #ifdef CTRL
  96. #undef CTRL
  97. #endif
  98.  
  99. #define CTRL(c) ((c) & (~control_character_bit))
  100. #define META(c) ((c) | meta_character_bit)
  101.  
  102. #define UNMETA(c) ((c) & (~meta_character_bit))
  103. #define UNCTRL(c) to_upper(((c)|control_character_bit))
  104.  
  105. #ifndef to_upper
  106. #define to_upper(c) (((c) < 'a' || (c) > 'z') ? c : c-32)
  107. #define to_lower(c) (((c) < 'A' || (c) > 'Z') ? c : c+32)
  108. #endif
  109.  
  110. #define CTRL_P(c) ((unsigned char) (c) < control_character_threshold)
  111. #define META_P(c) ((unsigned char) (c) > meta_character_threshold)
  112.  
  113. #define NEWLINE '\n'
  114. #define RETURN CTRL('M')
  115. #define DELETE 0x07f
  116. #define TAB '\t'
  117. #define ABORT_CHAR CTRL('G')
  118. #define PAGE CTRL('L')
  119. #define SPACE 0x020
  120. #define ESC CTRL('[')
  121. #define control_display_prefix '^'
  122.  
  123. #define TAG_TABLE_END_STRING "\037\nEND TAG TABLE"
  124. #define TAG_TABLE_BEG_STRING "\nTAG TABLE:\n"
  125. #define NODE_ID "Node:"
  126. #define NNODENAME 4        /* Default amount to grow nodename list by. */
  127. #define FILENAME_LEN 256
  128. #define NODENAME_LEN 256
  129. #define STRING_SIZE 256
  130. #define nodeend_sequence "\n\037"
  131.  
  132. /* All right, some windows stuff. */
  133.  
  134. typedef struct {
  135.   /* Absolute x and y coordinates for usable portion of this window. */
  136.   int left, top, right, bottom;
  137.   /* Absolute cursor position in this window. */
  138.   int ch, cv;
  139. } WINDOW;
  140.  
  141. typedef struct _wind_list {
  142.   int left, top, right, bottom;
  143.   int ch, cv;
  144.   struct _wind_list *next_window;
  145. } WINDOW_LIST;
  146.  
  147. WINDOW the_window = {0, 0, 80, 24, 0, 0};
  148. WINDOW_LIST *window_stack = (WINDOW_LIST *)NULL;
  149. WINDOW terminal_window = {0, 0, 80, 24, 0, 0};
  150.  
  151. /* Not really extern, but defined later in this file. */
  152. extern WINDOW echo_area;
  153. char *xmalloc (), *xrealloc (), *getenv ();
  154. char *next_info_file (), *opsys_filename ();
  155. boolean build_menu (), find_menu_node ();
  156. void swap_filestack (), pop_filestack ();
  157.  
  158. /* A crock, this should be done in a different way. */
  159. #define MAX_INDIRECT_FILES 100 
  160.  
  161. /* The info history list. */
  162. NODEINFO *Info_History = NULL;
  163.  
  164. /* ?Can't have more than xx files in the indirect list? */
  165. INDIRECT_INFO indirect_list[MAX_INDIRECT_FILES];
  166.  
  167. /* The filename of the currently loaded info file. */
  168. char current_info_file[FILENAME_LEN];
  169.  
  170. /* The nodename of the node the user is looking at. */
  171. char current_info_node[NODENAME_LEN];
  172.  
  173. /* The last file actually loaded.  Not the same as current info file. */
  174. char last_loaded_info_file[FILENAME_LEN];
  175.  
  176. /* Offsets in info_file of top and bottom of current_info_node. */
  177. int nodetop, nodebot;
  178.  
  179. /* Number of lines in this node. */
  180. int nodelines;
  181.  
  182. /* Buffer for the info file. */
  183. char *info_file = NULL;
  184.  
  185. /* Length of the above buffer. */
  186. int info_buffer_len;
  187.  
  188. /* Pointer to the start of a tag table, or NULL to show none. */
  189. char *tag_table = NULL;
  190.  
  191. /* Length of the above buffer. */
  192. int tag_buffer_len;        
  193.  
  194. /* If true, the tag table is indirect. */
  195. boolean indirect = false;
  196. int indirect_top;
  197.  
  198. /* Offset in the buffer of the current pagetop. */
  199. int pagetop;
  200.  
  201. /* Offset in the buffer of the last displayed character. */
  202. int pagebot = 0;
  203.  
  204. /* If non-NULL, this is a colon separated list of directories to search
  205.    for a specific info file.  The user places this variable into his or
  206.    her environment. */
  207. char *infopath = NULL;
  208.  
  209. /* If filled, the name of a file to write to. */
  210. char dumpfile[FILENAME_LEN];
  211.  
  212. /* This is the command to print a node. A default value is compiled in,
  213.    or it can be found from the environment as $INFO_PRINT_COMMAND. */
  214. char *print_command;
  215.  
  216. /* **************************************************************** */
  217. /*                                    */
  218. /*            Getting Started.                */
  219. /*                                    */
  220. /* **************************************************************** */
  221.  
  222. /* Begin the Info session. */
  223.  
  224. /* Global is on until we are out of trouble. */
  225. int totally_inhibit_errors = 1;
  226.  
  227. struct option long_options[] = {
  228.   {"directory", 1, 0, 'd'},
  229.   {"node", 1, 0, 'n'},
  230.   {"file", 1, 0, 'f'},
  231.   {"output", 1, 0, 'o'},
  232.   {NULL, 0, NULL, 0}
  233. };
  234.  
  235. #define savestring(x) strcpy (xmalloc (1 + strlen (x)), (x))
  236.  
  237. main (argc, argv)
  238.      int argc;
  239.      char **argv;
  240. {
  241.   int c, ind;
  242.   char filename[FILENAME_LEN];
  243.   char *nodename;
  244.   char **nodenames;
  245.   int nodenames_size, nodenames_index;
  246.   char *ptr, *env_infopath, *env_print_command;
  247.  
  248.   nodenames_index = 0;
  249.   nodenames = (char **)xmalloc ((nodenames_size = 1) * sizeof (char *));
  250.   nodenames[0] = (char *)NULL;
  251.  
  252.   env_infopath = getenv ("INFOPATH");
  253.   env_print_command = getenv ("INFO_PRINT_COMMAND");
  254.  
  255.   filename[0] = '\0';
  256.  
  257.   if (env_infopath && *env_infopath)
  258.     infopath = savestring (env_infopath);
  259.   else
  260.     infopath = savestring (DEFAULT_INFOPATH);
  261.  
  262.   if (env_print_command && *env_print_command)
  263.     print_command = savestring (env_print_command);
  264.   else
  265.     print_command = savestring (INFO_PRINT_COMMAND);
  266.  
  267.   while ((c = getopt_long (argc, argv, "d:n:f:o:", long_options, &ind)) != EOF)
  268.     {
  269.       if (c == 0 && long_options[ind].flag == 0)
  270.     c = long_options[ind].val;
  271.       switch (c)
  272.     {
  273.     case 0:
  274.       break;
  275.       
  276.     case 'd':
  277.       free (infopath);
  278.       infopath = savestring (optarg);
  279.       break;
  280.       
  281.     case 'n':
  282.  
  283.       if (nodenames_index + 2 > nodenames_size)
  284.         nodenames = (char **)
  285.           xrealloc (nodenames, (nodenames_size += 10) * sizeof (char *));
  286.  
  287.       nodenames[nodenames_index++] = optarg;
  288.       nodenames[nodenames_index] = (char *)NULL;
  289.       break;
  290.       
  291.     case 'f':
  292.       strncpy (filename, optarg, FILENAME_LEN);
  293.       break;
  294.       
  295.     case 'o':
  296.       strncpy (dumpfile, optarg, FILENAME_LEN);
  297.       break;
  298.       
  299.     default:
  300.       usage ();
  301.     }
  302.     }
  303.  
  304.   /* Okay, flags are parsed.  Get possible Info menuname. */
  305.  
  306.   if (*filename && (ptr = rindex (filename,'/')) != NULL )
  307.     {
  308.       /* Add filename's directory to path. */
  309.       char *temp;
  310.  
  311.       temp = (char *)xmalloc (2 + strlen (filename) + strlen (infopath));
  312.       strncpy (temp, filename, ptr - filename);
  313.       sprintf (temp + (ptr - filename), ":%s", infopath);
  314.       free (infopath);
  315.       infopath = temp;
  316.     }
  317.  
  318.   /* Start with DIR or whatever was specified. */
  319.   if (!get_node (filename, (nodenames[0] == NULL) ? "" : nodenames[0], false)
  320.       && !get_node ((char *)NULL, (char *)NULL, true))
  321.       {
  322.     if (!*filename)
  323.       strcpy (filename, "dir");
  324.  
  325.     fprintf (stderr, "%s: Cannot find \"%s\", anywhere along the\n",
  326.          argv[0], filename);
  327.     fprintf (stderr, "search path of \"%s\".\n", infopath);
  328.  
  329.     exit (1);
  330.       }
  331.  
  332.   totally_inhibit_errors = 0;
  333.  
  334.   for (ind = 1 ; ind < nodenames_index ; ind++)
  335.     get_node (filename, nodenames[ind], false);
  336.  
  337.   nodename = nodenames[nodenames_index > 0 ? nodenames_index - 1 : 0];
  338.   if (!nodename)
  339.     {
  340.       nodename = (char *)xmalloc (NODENAME_LEN);
  341.       *nodename = '\0';
  342.     }
  343.   
  344.   if (optind != argc)
  345.     {
  346.       putchar ('\n');
  347.  
  348.       while (optind != argc)
  349.     {
  350.       if (!build_menu ())
  351.           {
  352.           display_error ("There is no menu in node \"%s\"",
  353.                  current_info_node );
  354.           break;
  355.         }
  356.       else if (!find_menu_node (argv[optind], nodename))
  357.         {
  358.           display_error
  359.         ("There is no menu entry for \"%s\" in node \"%s\"",
  360.          argv[optind], nodename );
  361.           break;
  362.         }
  363.       else if (!get_node ((char *)NULL, nodename, false))
  364.         {
  365.           break;
  366.         }
  367.       else
  368.          {
  369. #if defined (NOTDEF)
  370.           /* RMS says not to type this stuff out because he expects
  371.          programs to call Info instead of interactive users. */
  372.           printf ("%s.. ",argv[optind]);
  373.           fflush (stdout);
  374. #endif
  375.           optind++;
  376.         }
  377.     }
  378.     }
  379.   begin_info_session ();
  380.   exit (0);
  381. }
  382.  
  383. usage ()
  384. {
  385.   fprintf (stderr,"%s\n%s\n%s\n%s\n",
  386. "Usage: info [-d dir-path] [-f info-file] [-n node-name -n node-name ...]",
  387. "            [-o output-file] [+directory dir-path] [+file info-file]",
  388. "            [+node node-name +node node-name ...]",
  389. "            [+output output-file] [menu-selection...]");
  390.   exit (1);
  391. }
  392.  
  393. #if defined (SIGTSTP)
  394. Function *old_tstp;
  395. Function *old_ttou, *old_ttin;
  396. #endif /* SIGTSTP */
  397.  
  398. #if defined (SIGWINCH)
  399. Function *old_winch;
  400. #endif /* SIGWINCH */
  401.  
  402. /* Start using Info. */
  403. begin_info_session ()
  404. {
  405.   int info_signal_handler ();
  406.  
  407.   /* If the user just wants to dump the node, then do that. */
  408.   if (dumpfile[0])
  409.     {
  410.       dump_current_node (dumpfile);
  411.       exit (0);
  412.     }
  413.  
  414.   init_terminal_io ();
  415.  
  416.   /* Install handlers for restoring/breaking the screen. */
  417.  
  418.   install_signals ();
  419.   new_echo_area ();
  420.  
  421.   print_string ("Welcome to Info!  Type \"?\" for help. ");
  422.   close_echo_area ();
  423.   toploop ();
  424.   goto_xy (the_window.left, the_window.bottom + 1);
  425.   restore_io ();
  426. }
  427.  
  428. /* What to do before processing a stop signal. */
  429. before_stop_signal ()
  430. {
  431.   restore_io ();
  432. }
  433.  
  434. /* What to do after processing a stop signal. */
  435. after_stop_signal ()
  436. {
  437.   clear_screen ();
  438.   display_page ();
  439.   goto_xy (the_window.ch, the_window.cv);
  440.   opsys_init_terminal ();
  441. }
  442.  
  443. /* Do the right thing with this signal. */
  444. info_signal_handler (sig)
  445.      int sig;
  446. {
  447.   switch (sig)
  448.     {
  449. #if defined (SIGTSTP)
  450.     case SIGTSTP:
  451.     case SIGTTOU:
  452.     case SIGTTIN:
  453.       before_stop_signal ();
  454.       signal (sig, SIG_DFL);
  455.       kill (getpid (), sig);
  456.       after_stop_signal ();
  457.       signal (sig, info_signal_handler);
  458.       break;
  459. #endif /* SIGTSTP */
  460.  
  461. #if defined (SIGWINCH)
  462.     case SIGWINCH:
  463.       /* Window has changed.  Get the size of the new window, and rebuild our
  464.          window chain. */
  465.       {
  466.     int display_page ();
  467.     extern char *widest_line;
  468.     extern WINDOW terminal_window;
  469.     extern WINDOW_LIST *window_stack;
  470.     extern int terminal_rows, terminal_columns;
  471.     int delta_width, delta_height, right, bottom;
  472.  
  473.     right = get_terminal_columns ();
  474.     bottom = get_terminal_rows ();
  475.  
  476.     delta_width = right - terminal_columns;
  477.     delta_height = bottom - terminal_rows;
  478.  
  479.     terminal_columns = right;
  480.     terminal_rows = bottom;
  481.  
  482.     /* Save current window, whatever it is. */
  483.     push_window ();
  484.  
  485.     /* Change the value of the widest_line. */
  486.     free (widest_line);
  487.     widest_line = (char *) xmalloc (right);
  488.  
  489.     /* Make the new window.  Map over all windows in window list. */
  490.     {
  491.       WINDOW_LIST *wind = window_stack;
  492.       extern WINDOW modeline_window;
  493.  
  494.       while (wind != (WINDOW_LIST *)NULL)
  495.         {
  496.           adjust_wind ((WINDOW *)wind, delta_width, delta_height);
  497.           wind = wind->next_window;
  498.         }
  499.  
  500.       /* Adjust the other windows that we know about. */
  501.       adjust_wind (&terminal_window, delta_width, delta_height);
  502.       adjust_wind (&echo_area, delta_width, delta_height);
  503.       adjust_wind (&modeline_window, delta_width, delta_height);
  504.     }
  505.  
  506.     /* Clear and redisplay the entire terminal window. */
  507.     set_window (&terminal_window);
  508.     clear_screen ();
  509.  
  510.     /* Redisplay the contents of the screen. */
  511.     with_output_to_window (&terminal_window, display_page);
  512.  
  513.     /* Get back the current window. */
  514.     pop_window ();
  515.       }
  516.       break;
  517. #endif /* SIGWINCH */
  518.  
  519.     case SIGINT:
  520.       restore_io ();
  521.       exit (1);
  522.       break;
  523.     }
  524. }
  525.  
  526. install_signals ()
  527. {
  528. #if defined (SIGTSTP)
  529.   old_tstp = (Function *) signal (SIGTSTP, info_signal_handler);
  530.   old_ttou = (Function *) signal (SIGTTOU, info_signal_handler);
  531.   old_ttin = (Function *) signal (SIGTTIN, info_signal_handler);
  532. #endif /* SIGTSTP */
  533.  
  534. #if defined (SIGWINCH)
  535.   old_winch = (Function *) signal (SIGWINCH, info_signal_handler);
  536. #endif /* SIGWINCH */
  537.  
  538.   signal (SIGINT, info_signal_handler);
  539. }
  540.  
  541. adjust_wind (wind, delta_width, delta_height)
  542.      WINDOW *wind;
  543.      int delta_width, delta_height;
  544. {
  545.   wind->right += delta_width;
  546.   wind->bottom += delta_height;
  547.   wind->ch += delta_width;
  548.   wind->cv += delta_height;
  549.  
  550.   /* Ugly hack to fix busted windows code.  If the window we are adjusting
  551.      already has a TOP offset, then adjust that also. */
  552.   if (wind->top)
  553.     wind->top += delta_height;
  554. }
  555.  
  556. /* **************************************************************** */
  557. /*                                    */
  558. /*            Completing Things                */
  559. /*                                    */
  560. /* **************************************************************** */
  561.  
  562. typedef struct completion_entry
  563. {
  564.   char *identifier;
  565.   char *data;
  566.   struct completion_entry *next;
  567. }                COMP_ENTRY;
  568.  
  569. /* The linked list of COMP_ENTRY structures that you create. */
  570. COMP_ENTRY *completion_list = (COMP_ENTRY *) NULL;
  571.  
  572. /* The vector of COMP_ENTRY pointers that COMPLETE returns. */
  573. COMP_ENTRY **completions = NULL;
  574.  
  575. /* The number of elements in the above vector. */
  576. int completion_count;
  577.  
  578. /* Initial size of COMPLETIONS array. */
  579. #define INITIAL_COMPLETIONS_CORE_SIZE 200
  580.  
  581. /* Current size of the completion array in core. */
  582. int completions_core_size = 0;
  583.  
  584. /* Ease the typing task.  Another name for the I'th
  585.    IDENTIFIER of COMPLETIONS. */
  586. #define completion_id(i) ((completions[(i)])->identifier)
  587.  
  588. /* The number of completions that can be present before the help
  589.    function starts asking you about whether it should print them
  590.    all or not. */
  591. int completion_query_threshold = 100;
  592.  
  593. free_completion_list ()
  594. {
  595.   COMP_ENTRY *temp;
  596.   while (completion_list)
  597.     {
  598.       temp = completion_list;
  599.  
  600.       if (completion_list->identifier)
  601.     free (completion_list->identifier);
  602.  
  603.       if (completion_list->data)
  604.     free (completion_list->data);
  605.  
  606.       completion_list = completion_list->next;
  607.       free (temp);
  608.     }
  609. }
  610.  
  611. /* Add a single completion to COMPLETION_LIST.
  612.    IDENTIFIER is the string that the user should type.
  613.    DATA should just be a pointer to some random data that you wish to
  614.    have associated with the identifier, but I'm too stupid for that, so
  615.    it must be a string as well.  This allocates the space for the strings
  616.    so you don't necessarily have to. */
  617. add_completion (identifier, data)
  618.      char *identifier, *data;
  619. {
  620.   COMP_ENTRY *temp = (COMP_ENTRY *) xmalloc (sizeof (COMP_ENTRY));
  621.  
  622.   temp->identifier = (char *) xmalloc (strlen (identifier) + 1);
  623.   strcpy (temp->identifier, identifier);
  624.  
  625.   temp->data = (char *) xmalloc (strlen (data) + 1);
  626.   strcpy (temp->data, data);
  627.  
  628.   temp->next = completion_list;
  629.   completion_list = temp;
  630. }
  631.  
  632. /* Function for reading a line.  Supports completion on COMPLETION_LIST
  633.    if you pass COMPLETING as true.  Prompt is either a prompt or NULL,
  634.    LINE is the place to store the characters that are read.  LINE may start
  635.    out already containing some characters; if so, they are printed.  MAXCHARS
  636.    tells how many characters can fit in the buffer at LINE.  READLINE returns
  637.    FALSE if the user types the abort character.  LINE is returned with a '\0'
  638.    at the end, not a '\n'.  */
  639. boolean
  640. readline (prompt, line, maxchars, completing)
  641.      char *prompt, *line;
  642.      int maxchars;
  643.      boolean completing;
  644. {
  645.   int character;
  646.   int readline_ch, readline_cv;
  647.   int current_len = strlen (line);
  648.   int just_completed = 0;        /* Have we just done a completion? */
  649.   int meta_flag = 0;
  650.  
  651.   new_echo_area ();
  652.  
  653.   if (prompt)
  654.     print_string (prompt);
  655.  
  656.   readline_ch = the_window.ch;
  657.   readline_cv = the_window.cv;
  658.  
  659.   print_string (line);
  660.  
  661.   while (true)
  662.     {
  663.       line[current_len] = '\0';
  664.       goto_xy (readline_ch, readline_cv);
  665.       print_string (line);
  666.       clear_eol ();
  667.  
  668.       if (just_completed)
  669.     just_completed--;
  670.  
  671.       character = blink_cursor ();
  672.       if (meta_flag)
  673.     {
  674.       character = META (character);
  675.       meta_flag = 0;
  676.     }
  677.  
  678.       if (META_P (character))
  679.     character = META (to_upper (UNMETA (character)));
  680.  
  681.       switch (character)
  682.     {
  683.     case EOF:
  684.       character = '\n';
  685.  
  686.     case ESC:
  687.       meta_flag++;
  688.       break;
  689.  
  690.     case META (DELETE):
  691.     case CTRL ('W'):
  692.       while (current_len && line[current_len] == SPACE)
  693.         current_len--;
  694.  
  695.       if (!current_len)
  696.         break;
  697.  
  698.       while (current_len && line[current_len] != SPACE)
  699.         current_len--;
  700.  
  701.       break;
  702.  
  703.     case CTRL ('U'):
  704.       current_len = 0;
  705.       break;
  706.  
  707.     case '\b':
  708.     case 0x07f:
  709.       if (current_len)
  710.         current_len--;
  711.       else
  712.         ding ();
  713.       break;
  714.  
  715.     case '\n':
  716.     case '\r':
  717.       if (completing)
  718.         {
  719.           extern int completion_count;
  720.  
  721.           try_complete (line);
  722.  
  723.           if (completion_count >= 1)
  724.         {
  725.           close_echo_area ();
  726.           return (true);
  727.         }
  728.           else
  729.         {
  730.           current_len = strlen (line);
  731.           break;
  732.         }
  733.         }
  734.       else
  735.         {
  736.           close_echo_area ();
  737.           return (true);
  738.         }
  739.  
  740.     case ABORT_CHAR:
  741.       ding ();
  742.  
  743.       if (current_len)
  744.         {
  745.           current_len = 0;
  746.         }
  747.       else
  748.         {
  749.           close_echo_area ();
  750.           clear_echo_area ();
  751.           return (false);
  752.         }
  753.       break;
  754.  
  755.     case ' ':
  756.     case '\t':
  757.     case '?':
  758.       if (completing)
  759.         {
  760.           extern int completion_count;
  761.  
  762.           if (character == '?' || just_completed)
  763.         {
  764.           help_possible_completions (line);
  765.           break;
  766.         }
  767.           else
  768.         {
  769.           char temp_line[NODENAME_LEN];
  770.           strcpy (temp_line, line);
  771.           try_complete (line); just_completed = 2;
  772.           if (completion_count != 1 && character == SPACE)
  773.             {
  774.               if (strcmp (temp_line, line) == 0)
  775.             {
  776.               line[current_len] = SPACE;
  777.               line[current_len + 1] = '\0';
  778.               strcpy (temp_line, line);
  779.               try_complete (line);
  780.               if (completion_count == 0)
  781.                 {
  782.                   line[current_len] = '\0';
  783.                   ding ();
  784.                 }
  785.             }
  786.             }
  787.           current_len = strlen (line);
  788.           if (completion_count == 0)
  789.             ding ();
  790.           break;
  791.         }
  792.         }
  793.       /* Do *NOT* put anything in-between the completing cases and
  794.          the default: case.  No.  Because the SPC, TAB and `?' get
  795.          treated as normal characters by falling through the
  796.          "if (completing)" test above. */
  797.     default:
  798.       if (!CTRL_P (character) &&
  799.           !META_P (character) &&
  800.           current_len < maxchars)
  801.         line[current_len++] = character;
  802.       else
  803.         ding ();
  804.     }
  805.     }
  806. }
  807.  
  808. /* Initialize whatever the completer is using. */
  809. init_completer ()
  810. {
  811.   if (completions_core_size != INITIAL_COMPLETIONS_CORE_SIZE)
  812.     {
  813.       if (completions)
  814.     free (completions);
  815.  
  816.       completions = (COMP_ENTRY **)
  817.     xmalloc ((sizeof (COMP_ENTRY *))
  818.          * (completions_core_size = INITIAL_COMPLETIONS_CORE_SIZE));
  819.     }
  820.   completion_count = 0;
  821. }
  822.  
  823. /* Reverse the completion list passed in LIST, and
  824.    return a pointer to the new head. */
  825. COMP_ENTRY *
  826. reverse_list (list)
  827.      COMP_ENTRY *list;
  828. {
  829.   COMP_ENTRY *next;
  830.   COMP_ENTRY *prev = (COMP_ENTRY *) NULL;
  831.  
  832.   while (list)
  833.     {
  834.       next = list->next;
  835.       list->next = prev;
  836.       prev = list;
  837.       list = next;
  838.     }
  839.   return (prev);
  840. }
  841.  
  842. /* Remember the possible completion passed in POINTER on the
  843.    completions list. */
  844. remember_completion (pointer)
  845.      COMP_ENTRY *pointer;
  846. {
  847.   if (completion_count == completions_core_size)
  848.     {
  849.       COMP_ENTRY **temp = (COMP_ENTRY **)
  850.     realloc (completions, ((sizeof (COMP_ENTRY *))
  851.                    * (completions_core_size +=
  852.                   INITIAL_COMPLETIONS_CORE_SIZE)));
  853.       if (!temp)
  854.     {
  855.       display_error ("Too many completions (~d)!  Out of core!",
  856.              completion_count);
  857.       return;
  858.     }
  859.       else
  860.     completions = temp;
  861.     }
  862.   completions[completion_count++] = pointer;
  863. }
  864.  
  865. /* Complete TEXT from identifiers in LIST.  Place the resultant
  866.    completions in COMPLETIONS, and the number of completions in
  867.    COMPLETION_COUNT. Modify TEXT to contain the least common
  868.    denominator of all the completions found. */
  869. boolean
  870. complete (text, list)
  871.      char *text;
  872.      COMP_ENTRY *list;
  873. {
  874.   int low_match, i, idx;
  875.   int string_length = strlen (text);
  876.  
  877.   init_completer ();
  878.   low_match = 100000;        /* Some large number. */
  879.  
  880.   while (list)
  881.     {
  882.       if (strnicmp (text, list->identifier, string_length) == 0)
  883.     remember_completion (list);
  884.       list = list->next;
  885.     }
  886.  
  887.   if (completion_count == 0)
  888.     return (false);
  889.  
  890.   if (completion_count == 1)
  891.     {                /* One completion */
  892.       strcpy (text, completion_id (0));
  893.       return (true);
  894.     }
  895.  
  896.   /* Else find the least common denominator */
  897.  
  898.   idx = 1;
  899.  
  900.   while (idx < completion_count)
  901.     {
  902.       int c1, c2;
  903.       for (i = 0;
  904.        (c1 = to_lower (completion_id (idx - 1)[i])) &&
  905.        (c2 = to_lower (completion_id (idx)[i]));
  906.        i++)
  907.     if (c1 != c2)
  908.       break;
  909.  
  910.       if (low_match > i)
  911.     low_match = i;
  912.       idx++;
  913.     }
  914.  
  915.   strncpy (text, completion_id (0), low_match);
  916.   text[low_match] = '\0';
  917.   return (true);
  918. }
  919.  
  920. /* Complete TEXT from the completion structures in COMPLETION_LIST. */
  921. boolean
  922. try_complete (text)
  923.      char *text;
  924. {
  925.   return (complete (text, completion_list));
  926. }
  927.  
  928. /* The function that prints out the possible completions. */
  929. help_possible_completions (text)
  930.      char *text;
  931. {
  932.   char temp_string[2000];
  933.  
  934.   goto_xy (the_window.left, the_window.top);
  935.   strcpy (temp_string, text);
  936.   try_complete (temp_string);
  937.  
  938.   open_typeout ();
  939.  
  940.   if (completion_count == 0)
  941.     {
  942.       print_string ("There are no possible completions.\n");
  943.       goto print_done;
  944.     }
  945.  
  946.   if (completion_count == 1)
  947.     {
  948.       print_string
  949.     ("The only possible completion of what you have typed is:\n\n");
  950.       print_string (completion_id(0));
  951.       goto print_done;
  952.     }
  953.  
  954.   if (completion_count >= completion_query_threshold)
  955.     {
  956.       print_string
  957.     ("\nThere are %d completions.  Do you really want to see them all",
  958.      completion_count);
  959.  
  960.       if (!get_y_or_n_p ())
  961.     return;
  962.     }
  963.  
  964.   print_string ("\nThe %d completions of what you have typed are:\n\n",
  965.         completion_count);
  966.  
  967.   {
  968.     int idx = 0;
  969.     int counter = 0;
  970.     int columns = (the_window.right - the_window.left) / 30;
  971.  
  972.     while (idx < completion_count)
  973.       {
  974.     if (counter == columns)
  975.       {
  976.         charout ('\n');
  977.         counter = 0;
  978.       }
  979.     indent_to (counter * 30);
  980.     print_string (completion_id (idx));
  981.     counter++;
  982.     idx++;
  983.       }
  984.   }
  985.  
  986. print_done:
  987.   print_string ("\n\n-----------------\n");
  988.   close_typeout ();
  989. }
  990.  
  991. /* Return the next file that should be searched, or NULL if we are at the end
  992.    of the info file. If the FILE argument is provided, begin the search there,
  993.    if REWIND is true start the search at the beginning of the list.
  994.  
  995.    The list is the one built by an indirect tag table, on the supposition
  996.    that those files form a logical set to search if we are in one of them.
  997.    If no such list is current (either it doesn't exist, or FILE isn't on
  998.    it) the search list is set to be last_loaded_info_file */
  999. char *
  1000. next_info_file (file, rewind)
  1001.      char *file;        /* file to set `next' to. May be NULL. */
  1002.      boolean rewind;        /* should I rewind the file list?  */
  1003. {
  1004.   static int index = -1;
  1005.  
  1006.   if (file != NULL)
  1007.     {
  1008.       char *ptr = rindex (file,'/');
  1009.  
  1010.       if (ptr != NULL)
  1011.     file = ptr + 1;
  1012.  
  1013.       for (index = 0;
  1014.        index < MAX_INDIRECT_FILES &&
  1015.        indirect_list[index].filename != (char *)NULL;
  1016.        index++)
  1017.     {
  1018.       if (strcmp (file, indirect_list[index].filename) == 0)
  1019.         return (file);
  1020.     }
  1021.  
  1022.       /* OK, we are not on the current indirect_list. This means that
  1023.      we have switched to another node that has no indirect list,
  1024.      so forget the old one. */
  1025.       for (index = 0;
  1026.        index < MAX_INDIRECT_FILES &&
  1027.        indirect_list[index].filename != (char *)NULL;
  1028.        index++)
  1029.     {
  1030.       free (indirect_list[index].filename);
  1031.       indirect_list[index].filename = (char *)NULL;
  1032.     }
  1033.       return (indirect_list[0].filename = savestring (file));
  1034.     }
  1035.   else if (rewind)
  1036.     {
  1037.       index = 0;
  1038.  
  1039.       if (indirect_list[0].filename == (char *)NULL)
  1040.     indirect_list[0].filename = savestring (last_loaded_info_file);
  1041.     }
  1042.   else
  1043.     index++;
  1044.   
  1045.   if (index < MAX_INDIRECT_FILES &&
  1046.       indirect_list[index].filename != (char *)NULL)
  1047.     return (indirect_list[index].filename);
  1048.  
  1049.   index = -1;
  1050.   return (NULL);
  1051. }
  1052.  
  1053. /* **************************************************************** */
  1054. /*                                    */
  1055. /*            Getting Nodes                    */
  1056. /*                                    */
  1057. /* **************************************************************** */
  1058.  
  1059. /* A node name looks like:
  1060.    Node: nodename with spaces but not a comma,
  1061. or Node: (filename-containing-node)node-within-file
  1062. or Node: (filename)
  1063.  
  1064.    The latter case implies a nodename of "Top".  All files are
  1065.    supposed to have one.
  1066.  
  1067.    Lastly, the nodename specified could be "*", which specifies the
  1068.    entire file. */
  1069.  
  1070. /* Return the directory portion of FILENAME, i.e., everything before the
  1071.    last slash. */
  1072. static char *
  1073. file_directory (filename)
  1074.      char *filename;
  1075. {
  1076.   register char *scan;
  1077.   register int length;
  1078.   char *result;
  1079.  
  1080.   scan = filename;
  1081.  
  1082.   while (*scan++ != '\0');
  1083.  
  1084.   while (1)
  1085.     {
  1086.       if (scan == filename)
  1087.     break;
  1088.  
  1089.       if ((*--scan) == '/')
  1090.     {
  1091.       scan++;
  1092.       break;
  1093.     }
  1094.     }
  1095.  
  1096.   length = (scan - filename);
  1097.   result = (xmalloc (length + 1));
  1098.   strncpy (result, filename, length);
  1099.   result[length] = '\0';
  1100.  
  1101.   return (result);
  1102. }
  1103.  
  1104. /* Given FILENAME and DIRECTORY return a newly allocated string which
  1105.    is either the two concatenated, or simply FILENAME if it is absolute
  1106.    already. */
  1107. static char *
  1108. file_absolutize (filename, directory)
  1109.      char *filename, *directory;
  1110. {
  1111.   register int filename_len, directory_len;
  1112.   char *result;
  1113.  
  1114.   if (filename[0] == '/')
  1115.     return (savestring (filename));
  1116.  
  1117.   filename_len = strlen (filename);
  1118.   directory_len = strlen (directory);
  1119.   result = xmalloc (directory_len + filename_len + 1);
  1120.  
  1121.   strcpy (result, directory);
  1122.   strcat (result, filename);
  1123.  
  1124.   return (result);
  1125. }
  1126.  
  1127. /* Load FILENAME.  If REMEMBER_NAME is true, then remember the
  1128.    loaded filename in CURRENT_INFO_FILE.  In either case, remember
  1129.    the name of this file in LAST_LOADED_INFO_FILE. */
  1130. boolean
  1131. get_info_file (filename, remember_name)
  1132.      char *filename;
  1133.      boolean remember_name;
  1134. {
  1135.   FILE *input_stream;
  1136.   struct stat file_info;
  1137.   int pointer, result;
  1138.   char tempname[FILENAME_LEN];
  1139.  
  1140.   /* Get real filename. */
  1141.   strcpy (tempname, opsys_filename (filename));
  1142.  
  1143.   /* See if the file exists. */
  1144.   if ((result = stat (tempname, &file_info)) != 0)
  1145.     {
  1146.       /* Try again, this time with the name in lower-case. */
  1147.       char temp_tempname[FILENAME_LEN];
  1148.       int i;
  1149.  
  1150.       for (i = 0; temp_tempname[i] = to_lower (tempname[i]); i++);
  1151.       strcpy (temp_tempname, opsys_filename (temp_tempname));
  1152.  
  1153.       result = stat (temp_tempname, &file_info);
  1154.       if (!result)
  1155.     strcpy (tempname, temp_tempname);
  1156.     }
  1157.  
  1158.   /* See if this file is the last loaded one. */
  1159.   if (!result && (strcmp (last_loaded_info_file, tempname) == 0))
  1160.     return (true);
  1161.  
  1162.   /* Now try to open the file. */
  1163.   if (result || (input_stream = fopen (tempname, "r")) == NULL)
  1164.     {
  1165.       file_error (tempname);
  1166.       return (false);
  1167.     }
  1168.  
  1169.   /* If we already have a file loaded, then free it first. */
  1170.   if (info_file)
  1171.     {
  1172.       free (info_file);
  1173.  
  1174.       if (!indirect)
  1175.     {
  1176.       /* Then the tag table is also no longer valid. */
  1177.       tag_table = (char *) NULL;
  1178.     }
  1179.     }
  1180.  
  1181.   /* Read the contents of the file into a new buffer. */
  1182.  
  1183.   info_file = (char *) xmalloc (info_buffer_len = file_info.st_size);
  1184.   fread (info_file, 1, info_buffer_len, input_stream);
  1185.   fclose (input_stream);
  1186.   strcpy (last_loaded_info_file, tempname);
  1187.   if (remember_name)
  1188.     {
  1189.       strcpy (current_info_file, tempname);
  1190.       if (indirect)
  1191.     {
  1192.       int idx;
  1193.       indirect = false;
  1194.       free (tag_table);
  1195.     }
  1196.     }
  1197.   else
  1198.     return (true);
  1199.  
  1200.   /* The file has been read, and we don't know anything about it.
  1201.      Find out if it contains a tag table. */
  1202.  
  1203.   tag_table = NULL;        /* assume none. */
  1204.   indirect = false;
  1205.   tag_buffer_len = 0;
  1206.  
  1207.   set_search_constraints (info_file, info_buffer_len);
  1208.  
  1209.   /* Go to the last few lines in the file. */
  1210.   pointer = back_lines (8, info_buffer_len);
  1211.   pointer = search_forward (TAG_TABLE_END_STRING, pointer);
  1212.  
  1213.   if (pointer > -1)
  1214.     {
  1215.       /* Then there is a tag table.  Find the start of it,
  1216.      and remember that. */
  1217.       pointer = search_backward (TAG_TABLE_BEG_STRING, pointer);
  1218.  
  1219.       /* Handle error for malformed info file. */
  1220.       if (pointer < 0)
  1221.     display_error ("Start of tag table not found!");
  1222.       else
  1223.     {
  1224.       /* No problem.  If this file is an indirect file, then the contents
  1225.          of the tag table must remain in RAM the entire time.  Otherwise,
  1226.          we can flush the tag table with the file when the file is flushed.
  1227.          So, if indirect, remember that, and copy the table to another
  1228.          place.*/
  1229.  
  1230.       int indirect_check = forward_lines (2, pointer);
  1231.  
  1232.       tag_table = info_file + pointer;
  1233.       tag_buffer_len = info_buffer_len - pointer;
  1234.  
  1235.       /* Shorten the search constraints. */
  1236.       info_buffer_len = pointer;
  1237.  
  1238.       if (looking_at ("(Indirect)\n", indirect_check))
  1239.         {
  1240.           /* We have to find the start of the indirect file's
  1241.          information. */
  1242.           tag_table = (char *) xmalloc (tag_buffer_len);
  1243.  
  1244.           bcopy (&info_file[indirect_check], tag_table, tag_buffer_len);
  1245.  
  1246.           /* Find the list of filenames. */
  1247.           indirect_top = search_backward ("Indirect:\n", indirect_check);
  1248.           if (indirect_top < 0)
  1249.         {
  1250.           free (tag_table);
  1251.           tag_table = (char *) NULL;
  1252.           display_error ("Start of INDIRECT tag table not found!");
  1253.           return (false);
  1254.         }
  1255.  
  1256.           /* Remember the filenames, and their byte offsets. */
  1257.           {
  1258.         /* Index into the filename/offsets array. */
  1259.         int idx, temp_first_byte;
  1260.         char temp_filename[FILENAME_LEN];
  1261.         char *directory = file_directory (tempname);
  1262.  
  1263.         info_buffer_len = indirect_top;
  1264.  
  1265.         /* For each line, scan the info into globals.  Then save
  1266.                the information in the INDIRECT_INFO structure. */
  1267.  
  1268.         for (idx = 0; idx < MAX_INDIRECT_FILES &&
  1269.              indirect_list[idx].filename != (char *) NULL;
  1270.              idx++)
  1271.             {
  1272.              free (indirect_list[idx].filename);
  1273.              indirect_list[idx].filename = (char *) NULL;
  1274.           }
  1275.         
  1276.         for (idx = 0;info_file[indirect_top] != info_separator_char &&
  1277.              idx < MAX_INDIRECT_FILES;)
  1278.           {
  1279.             indirect_top = forward_lines (1, indirect_top);
  1280.             if (info_file[indirect_top] == info_separator_char)
  1281.               break;
  1282.  
  1283.             /* Ignore blank lines. */
  1284.             if (info_file[indirect_top] == '\n')
  1285.               continue;
  1286.  
  1287.             sscanf (&info_file[indirect_top], "%s%d",
  1288.                 temp_filename, &temp_first_byte);
  1289.  
  1290.             if (strlen (temp_filename))
  1291.               {
  1292.             temp_filename[strlen (temp_filename) - 1] = '\0';
  1293.             indirect_list[idx].filename =
  1294.               file_absolutize (temp_filename, directory);
  1295.             indirect_list[idx].first_byte = temp_first_byte;
  1296.             idx++;
  1297.               }
  1298.           }
  1299.  
  1300.         free (directory);
  1301.  
  1302.         /* Terminate the table. */
  1303.         if (idx == MAX_INDIRECT_FILES)
  1304.           {
  1305.             display_error
  1306.               ("Sorry, the INDIRECT file array isn't large enough.");
  1307.             idx--;
  1308.           }
  1309.         indirect_list[idx].filename = (char *) NULL;
  1310.           }
  1311.           indirect = true;
  1312.        } else {
  1313.           ;
  1314.        }
  1315.     }
  1316.     }
  1317.   return (true);
  1318. }
  1319.  
  1320. /* Make current_info_node be NODENAME.  This could involve loading
  1321.    a file, etc.  POPPING is true if we got here because we are popping
  1322.    one level. */
  1323. boolean
  1324. get_node (filename, nodename, popping)
  1325.      char *nodename, *filename;
  1326.      boolean popping;
  1327. {
  1328.   int pointer;
  1329.   char internal_filename[FILENAME_LEN];
  1330.   char internal_nodename[NODENAME_LEN];
  1331.  
  1332.   if (nodename && *nodename)
  1333.     {
  1334.       /* Maybe nodename looks like: (filename)nodename, or worse: (filename).
  1335.          If so, extract the stuff out. */
  1336.       if (*nodename == '(')
  1337.     {
  1338.       int temp = 1, temp1 = 0;
  1339.       char character;
  1340.  
  1341.       filename = internal_filename;
  1342.  
  1343.       while ((character = nodename[temp]) && character != ')')
  1344.         {
  1345.           filename[temp - 1] = character;
  1346.           temp++;
  1347.         }
  1348.       filename[temp - 1] = '\0';
  1349.       temp++;            /* skip the closing ')' */
  1350.  
  1351.       /* We have the filename now.  The nodename follows. */
  1352.       internal_nodename[0] = '\0';
  1353.  
  1354.       while (nodename[temp] == ' ' ||
  1355.          nodename[temp] == '\t' ||
  1356.          nodename[temp] == '\n')
  1357.         temp++;
  1358.  
  1359.       if (nodename[temp])
  1360.         while (internal_nodename[temp1++] = nodename[temp++])
  1361.           ;
  1362.       else if (*filename != '\0')
  1363.         strcpy (internal_nodename,"Top");
  1364.  
  1365.       nodename = internal_nodename;
  1366.     }
  1367.     }
  1368.  
  1369.   if (!popping)
  1370.     push_node (current_info_file, current_info_node, pagetop, nodetop);
  1371.  
  1372.   if (!nodename || !*nodename)
  1373.     {
  1374.       nodename = internal_nodename;
  1375.       strcpy (nodename, "Top");
  1376.     }
  1377.  
  1378.   if (!filename || !*filename)
  1379.     {
  1380.       filename = internal_filename;
  1381.       strcpy (filename, current_info_file);
  1382.     }
  1383.  
  1384.   if (!*filename)
  1385.     strcpy (filename, "dir");
  1386.  
  1387.   if (!get_info_file (filename, true))
  1388.     goto node_not_found;
  1389.  
  1390.   if (strcmp (nodename, "*") == 0)
  1391.     {
  1392.       /* The "node" that we want is the entire file. */
  1393.       pointer = 0;
  1394.       goto found_node;
  1395.     }
  1396.   
  1397.   /* If we are using a tag table, see if we can find the nodename in it. */
  1398.   if (tag_table)
  1399.     {
  1400.       pointer = find_node_in_tag_table (nodename, 0);
  1401.       if (pointer < 1)
  1402.     {
  1403.       boolean pop_node ();
  1404.  
  1405.       /* The search through the tag table failed.  Maybe we
  1406.          should try searching the buffer?  Nahh, just barf. */
  1407.     node_not_found:
  1408.       if (popping)
  1409.         return (false);    /* Second time through. */
  1410.  
  1411.       {
  1412.          int save_inhibit = totally_inhibit_errors;
  1413.  
  1414.          totally_inhibit_errors = 0;
  1415.          display_error
  1416.            ("Sorry, unable to find the node \"%s\" in the file \"%s\".",
  1417.         nodename, filename);
  1418.          totally_inhibit_errors = save_inhibit;
  1419.       }
  1420.  
  1421.       current_info_file[0] = '\0';
  1422.       current_info_node[0] = '\0';
  1423.       last_loaded_info_file[0] = '\0';
  1424.       pop_node (internal_filename, internal_nodename, &nodetop, &pagetop);
  1425.       get_node (internal_filename, internal_nodename, true);
  1426.       return (false);
  1427.     }
  1428.  
  1429.       /* Otherwise, the desired location is right here.
  1430.          Scarf the position byte. */
  1431.       while (tag_table[pointer] != '\177')
  1432.     pointer++;
  1433.  
  1434.       sscanf (&tag_table[pointer + 1], "%d", &pointer);
  1435.  
  1436.       /* Okay, we have a position pointer.  If this is an indirect file,
  1437.          then we should look through the indirect_list for the first
  1438.          element.first_byte which is larger than this.  Then we can load
  1439.          the specified file, and win. */
  1440.       if (indirect)
  1441.     {
  1442.       /* Find the filename for this node. */
  1443.       int idx;
  1444.       for (idx = 0; idx < MAX_INDIRECT_FILES &&
  1445.            indirect_list[idx].filename != (char *) NULL; idx++)
  1446.         {
  1447.           if (indirect_list[idx].first_byte > pointer)
  1448.         {
  1449.           /* We found it. */
  1450.           break;
  1451.         }
  1452.         }
  1453.       if (!get_info_file (indirect_list[idx - 1].filename, true))
  1454.         goto node_not_found;
  1455.       pointer -= indirect_list[idx - 1].first_byte;
  1456.  
  1457.       /* Here is code to compensate for the header of an indirect file. */
  1458.       {
  1459.         int tt = find_node_start (0);
  1460.         if (tt > -1)
  1461.           pointer += tt;
  1462.       }
  1463.     }
  1464.       else
  1465.     {
  1466.       /* This tag table is *not* indirect.  The filename of the file
  1467.          containing this node is the same as the current file.  The
  1468.          line probably looks like:
  1469.          File: info,  Node: Checking25796 */
  1470.     }
  1471.     }
  1472.   else
  1473.     {
  1474.       /* We don't have a tag table.  The node can only be found by
  1475.          searching this file in its entirety.  */
  1476.       get_info_file (filename, true);
  1477.       pointer = 0;
  1478.     }
  1479.  
  1480.   /* Search this file, using pointer as a good guess where to start. */
  1481.   /* This is the same number that RMS used.  It might be right or wrong. */
  1482.   pointer -= 1000;
  1483.   if (pointer < 0)
  1484.     pointer = 0;
  1485.  
  1486.   pointer = find_node_in_file (nodename, pointer);
  1487.   if (pointer < 0)
  1488.     goto node_not_found;
  1489.  
  1490.   /* We found the node in it's file.  Remember exciting information. */
  1491.  
  1492. found_node:
  1493.   back_lines (0, pointer);
  1494.   nodetop = pagetop = pointer;
  1495.   strcpy (current_info_node, nodename);
  1496.   strcpy (current_info_file, filename);
  1497.   get_node_extent ();
  1498.   return (true);
  1499. }
  1500.  
  1501. /* Get the bounds for this node.  NODETOP points to the start of the
  1502.    node. Scan forward looking for info_separator_char, and remember
  1503.    that in NODEBOT. */
  1504. get_node_extent ()
  1505. {
  1506.   int idx = nodetop;
  1507.   int character;
  1508.   boolean do_it_till_end = (strcmp (current_info_node, "*") == 0);
  1509.  
  1510.   nodelines = 0;
  1511.  
  1512. again:
  1513.   while ((idx < info_buffer_len) &&
  1514.      ((character = info_file[idx]) != info_separator_char))
  1515.     {
  1516.       if (character == '\n')
  1517.     nodelines++;
  1518.       idx++;
  1519.     }
  1520.   if (do_it_till_end && idx != info_buffer_len)
  1521.     {
  1522.       idx++;
  1523.       goto again;
  1524.     }
  1525.   nodebot = idx;
  1526. }
  1527.  
  1528. /* Locate the start of a node in the current search_buffer.  Return
  1529.    the offset to the node start, or minus one.  START is the place in
  1530.    the file at where to begin the search. */
  1531. find_node_start (start)
  1532.      int start;
  1533. {
  1534.   return (search_forward (start_of_node_string, start));
  1535. }
  1536.  
  1537. /* Find NODENAME in TAG_TABLE. */
  1538. find_node_in_tag_table (nodename, offset)
  1539.      char *nodename;
  1540.      int offset;
  1541. {
  1542.   int temp;
  1543.  
  1544.   set_search_constraints (tag_table, tag_buffer_len);
  1545.  
  1546.   temp = offset;
  1547.   while (true)
  1548.     {
  1549.       offset = search_forward (NODE_ID, temp);
  1550.  
  1551.       if (offset < 0)
  1552.     return (offset);
  1553.  
  1554.       temp = skip_whitespace (offset + strlen (NODE_ID));
  1555.  
  1556.       if (strnicmp (tag_table + temp, nodename, strlen (nodename)) == 0)
  1557.     if (*(tag_table + temp + strlen (nodename)) == '\177')
  1558.       return (temp);
  1559.     }
  1560. }
  1561.  
  1562. /* Find NODENAME in INFO_FILE. */
  1563. find_node_in_file (nodename, offset)
  1564.      char *nodename;
  1565.      int offset;
  1566. {
  1567.   int temp, last_offset = -1;
  1568.  
  1569.   set_search_constraints (info_file, info_buffer_len);
  1570.  
  1571.   while (true)
  1572.     {
  1573.       offset = find_node_start (offset);
  1574.  
  1575.       if (offset == last_offset)
  1576.     offset = -1;
  1577.       else
  1578.     last_offset = offset;
  1579.  
  1580.       if (offset < 0)
  1581.     return (offset);
  1582.       else
  1583.     temp = forward_lines (1, offset);
  1584.  
  1585.       if (temp == offset)
  1586.     return (-1);        /* At last line now, just a node start. */
  1587.       else
  1588.     offset = temp;
  1589.  
  1590.       temp = string_in_line (NODE_ID, offset);
  1591.  
  1592.       if (temp > -1)
  1593.     {
  1594.       temp = skip_whitespace (temp + strlen (NODE_ID));
  1595.       if (strnicmp (info_file + temp, nodename, strlen (nodename)) == 0)
  1596.         {
  1597.           int check_exact = *(info_file + temp + strlen (nodename));
  1598.  
  1599.           if (check_exact == '\t' ||
  1600.           check_exact == ',' ||
  1601.           check_exact == '.' ||
  1602.           check_exact == '\n')
  1603.         return (offset);
  1604.         }
  1605.     }
  1606.     }
  1607. }
  1608.  
  1609.  
  1610. /* **************************************************************** */
  1611. /*                                    */
  1612. /*            Dumping and Printing Nodes                */
  1613. /*                                    */
  1614. /* **************************************************************** */
  1615.  
  1616. /* Make a temporary filename based on STARTER and the PID of this Info. */
  1617. char *
  1618. make_temp_filename (starter)
  1619.      char *starter;
  1620. {
  1621.   register int i;
  1622.   char *temp;
  1623.  
  1624.   temp = (char *)xmalloc (strlen (starter) + 10);
  1625.   sprintf (temp, "%s-%d", starter, getpid ());
  1626.  
  1627.   for (i = 0; temp[i]; i++)
  1628.     if (!isalnum (temp[i]))
  1629.       temp[i] == '-';
  1630.  
  1631.   return (temp);
  1632. }
  1633.  
  1634. /* Delete a file.  Print errors if necessary. */
  1635. deletefile (filename)
  1636.      char *filename;
  1637. {
  1638.   if (unlink (filename) != 0)
  1639.     {
  1640.       file_error (filename);
  1641.       return (1);
  1642.     }
  1643.   return (0);
  1644. }
  1645.  
  1646. printfile (filename)
  1647.      char *filename;
  1648. {
  1649.   int length = strlen (print_command) + strlen (filename) + strlen ("\n") + 1;
  1650.   char *command = (char *) xmalloc (length);
  1651.   int error;
  1652.  
  1653.   display_error ("Printing file `%s'...\n", filename);
  1654.   sprintf (command, "%s %s", print_command, filename);
  1655.   error = system (command);
  1656.   if (error)
  1657.     display_error ("Can't invoke `%s'", command);
  1658.   free (command);
  1659.   return (error);
  1660. }
  1661.  
  1662. /* Dump the current node into a file named FILENAME.
  1663.    Return 0 if the dump was successful, otherwise,
  1664.    print error and exit. */
  1665. dump_current_node (filename)
  1666.      char *filename;
  1667. {
  1668.   int i = nodetop;
  1669.   int c;
  1670.   FILE *output_stream = fopen (filename, "w");
  1671.   if (output_stream == (FILE *) NULL)
  1672.     {
  1673.       file_error (filename);
  1674.       return (1);
  1675.     }
  1676.  
  1677.   while (i < nodebot && i < info_buffer_len)
  1678.     {
  1679.       c = info_file[i];
  1680.       if (c < 0x020 && !(index ("\n\t\f", c)))
  1681.     {
  1682.       putc ('^', output_stream);
  1683.     }
  1684.       if (putc (info_file[i++], output_stream) == EOF)
  1685.     {
  1686.       fclose (output_stream);
  1687.       file_error (filename);
  1688.       return (1);
  1689.     }
  1690.     }
  1691.   fclose (output_stream);
  1692.   return (0);
  1693. }
  1694.  
  1695. /* **************************************************************** */
  1696. /*                                    */
  1697. /*             Toplevel eval loop.                 */
  1698. /*                                    */
  1699. /* **************************************************************** */
  1700.  
  1701. #define MENU_HEADER "\n* Menu:"
  1702. #define MENU_ID "\n* "
  1703. #define FOOTNOTE_HEADER "*Note"
  1704.  
  1705. /* Number of items that the current menu has. */
  1706. int the_menu_size = 0;
  1707.  
  1708. /* The node that last made a menus completion list. */
  1709. char menus_nodename[NODENAME_LEN];
  1710. char menus_filename[NODENAME_LEN];
  1711.  
  1712. boolean window_bashed = false;
  1713. static int search_start = 0;
  1714.  
  1715. /* The default prompt string for the Follow Reference command. */
  1716. char *visible_footnote = (char *)NULL;
  1717. toploop ()
  1718. {
  1719.   boolean done = false;
  1720.   boolean inhibit_display = false;
  1721.   int command = '\0', last_command = '\0';
  1722.   int last_pointer, count, new_ypos, last_pagetop;
  1723.   char nodename[NODENAME_LEN];
  1724.  
  1725.   new_ypos = last_pagetop = -1;
  1726.  
  1727.   while (!done)
  1728.     {
  1729.       if (!inhibit_display &&
  1730.       (window_bashed || (pagetop != last_pagetop)))
  1731.     display_page ();
  1732.  
  1733.       inhibit_display = window_bashed = false;
  1734.       last_pagetop = pagetop;
  1735.  
  1736.       nodename[0] = '\0';    /* Don't display old text in input line. */
  1737.  
  1738.       last_command = command;
  1739.  
  1740.       if (last_command == 'S')
  1741.     cursor_to (search_start);
  1742.       else
  1743.     goto_xy (echo_area.left, echo_area.top);
  1744.  
  1745.       command = blink_cursor ();
  1746.       clear_echo_area ();
  1747.  
  1748.       if (command == EOF)
  1749.     {
  1750.       done = true;
  1751.       continue;
  1752.     }
  1753.       command = to_upper (command);
  1754.  
  1755.       switch (command)
  1756.     {
  1757.     case 'D':
  1758.       get_node ((char *) NULL, "(dir)Top", false);
  1759.       break;
  1760.  
  1761.     case 'H':
  1762.       if ((the_window.bottom - the_window.top) < 24)
  1763.         get_node ((char *) NULL, "(info)Help-Small-Screen", false);
  1764.       else
  1765.         get_node ((char *) NULL, "(info)Help", false);
  1766.       break;
  1767.  
  1768.     case 'N':
  1769.       if (!next_node ())
  1770.         {
  1771.           display_error ("No NEXT for this node!");
  1772.           inhibit_display = true;
  1773.         }
  1774.       break;
  1775.  
  1776.     case 'P':
  1777.       if (!prev_node ())
  1778.         {
  1779.           display_error ("No PREV for this node!");
  1780.           inhibit_display = true;
  1781.         }
  1782.       break;
  1783.  
  1784.     case 'U':
  1785.       {
  1786.         int savetop = pagetop;
  1787.  
  1788.         if (!up_node ())
  1789.           {
  1790.         display_error ("No UP for this node!");
  1791.         inhibit_display = true;
  1792.         pagetop = savetop;
  1793.           }
  1794.         break;
  1795.       }
  1796.  
  1797.     case 'M':
  1798.       if (!build_menu ())
  1799.         {
  1800.           display_error ("No menu in this node!");
  1801.           inhibit_display = true;
  1802.           break;
  1803.         }
  1804.  
  1805.       if (!readline ("Menu item: ", nodename, NODENAME_LEN, true))
  1806.         {
  1807.           clear_echo_area ();
  1808.           inhibit_display = true;
  1809.           break;
  1810.         }
  1811.  
  1812.       I_goto_xy (echo_area.left, echo_area.top);
  1813.       if (!find_menu_node (nodename, nodename))
  1814.         {
  1815.           display_error ("\"%s\" is not a menu item!", nodename);
  1816.           inhibit_display = true;
  1817.           break;
  1818.         }
  1819.  
  1820.       if (get_node ((char *) NULL, nodename, false))
  1821.         clear_echo_area ();
  1822.       break;
  1823.  
  1824.     case 'F':
  1825.       {
  1826.         char footnote[NODENAME_LEN];
  1827.         if (!build_notes ())
  1828.           {
  1829.         display_error ("No cross-references in this node!");
  1830.         inhibit_display = true;
  1831.         break;
  1832.           }
  1833.         strcpy (footnote, visible_footnote);
  1834.         if (!readline ("Follow reference: ", footnote, NODENAME_LEN, true))
  1835.           {
  1836.         inhibit_display = true;
  1837.         break;
  1838.           }
  1839.  
  1840.         I_goto_xy (echo_area.left, echo_area.top);
  1841.         if (!find_note_node (footnote, nodename))
  1842.           {
  1843.         display_error ("\"%s\" is not a cross-reference in this node!",
  1844.                    footnote);
  1845.         inhibit_display = true;
  1846.         break;
  1847.           }
  1848.  
  1849.         if (get_node ((char *)NULL, nodename, false))
  1850.           clear_echo_area ();
  1851.         break;
  1852.       }
  1853.  
  1854.     case 'L':
  1855.       {
  1856.         char filename[FILENAME_LEN], nodename[NODENAME_LEN];
  1857.         int ptop, ntop;
  1858.         if (pop_node (filename, nodename, &ntop, &ptop) &&
  1859.         get_node (filename, nodename, true))
  1860.           {
  1861.         pagetop = ptop;
  1862.           }
  1863.         else
  1864.           inhibit_display = true;
  1865.         break;
  1866.       }
  1867.  
  1868.     case SPACE:
  1869.     case CTRL ('V'):
  1870.       if (!next_page ())
  1871.         {
  1872.           display_error ("At last page of this node now!");
  1873.           inhibit_display = true;
  1874.         }
  1875.       break;
  1876.  
  1877.     case DELETE:
  1878.       if (!prev_page ())
  1879.         {
  1880.           display_error ("At first page of this node now!");
  1881.           inhibit_display = true;
  1882.         }
  1883.       break;
  1884.  
  1885.     case 'B':
  1886.       if (pagetop == nodetop)
  1887.         {
  1888.           display_error ("Already at beginning of this node!");
  1889.           inhibit_display = true;
  1890.         }
  1891.       else
  1892.         pagetop = nodetop;
  1893.       break;
  1894.  
  1895.       /* I don't want to do this this way, but the documentation
  1896.          clearly states that '6' doesn't work.  It states this for a
  1897.          reason, and ours is not to wonder why... */
  1898.     case '1':
  1899.     case '2':
  1900.     case '3':
  1901.     case '4':
  1902.     case '5':
  1903.       {
  1904.         int item = command - '0';
  1905.  
  1906.         if (!build_menu ())
  1907.           {
  1908.         display_error ("No menu in this node!");
  1909.         inhibit_display = true;
  1910.         break;
  1911.           }
  1912.  
  1913.         if (item > the_menu_size)
  1914.           {
  1915.         display_error ("There are only %d items in the menu!",
  1916.                    the_menu_size);
  1917.         inhibit_display = true;
  1918.         break;
  1919.           }
  1920.  
  1921.         if (!get_menu (item))
  1922.           inhibit_display = true;
  1923.       }
  1924.       break;
  1925.  
  1926.     case 'G':
  1927.       if (!readline ("Goto node: ", nodename, NODENAME_LEN, false))
  1928.         {
  1929.           inhibit_display = true;
  1930.           break;
  1931.         }
  1932.  
  1933.       if (get_node ((char *) NULL, nodename, false))
  1934.         clear_echo_area ();
  1935.       break;
  1936.  
  1937.       /* Search from the starting position forward for a string.
  1938.          Select the node containing the desired string.  Put the
  1939.          top of the page screen_lines / 2 lines behind it, but not
  1940.          before nodetop. */
  1941.     case 'S':
  1942.       {
  1943.         extern int info_buffer_len;
  1944.         int pointer, temp;
  1945.          char prompt[21 + NODENAME_LEN + 1];
  1946.          static char search_string[NODENAME_LEN] = "";
  1947.          static char *starting_filename = NULL,
  1948.                 *starting_nodename = NULL;
  1949.         static int starting_pagetop = 0;
  1950.         static boolean wrap_search = false;
  1951.  
  1952.          sprintf (prompt, "Search for string [%s]: ", search_string);
  1953.  
  1954.          if (!readline (prompt , nodename, NODENAME_LEN, false))
  1955.           {
  1956.         inhibit_display = true;
  1957.         break;
  1958.           }
  1959.  
  1960.         /* If the user defaulted the search string, and the previous
  1961.            command was search, then this is a continuation of the
  1962.            previous search. */
  1963.         if (((strcmp (nodename, search_string) == 0) ||
  1964.          (!*nodename && *search_string)) &&
  1965.         (last_command == 'S'))
  1966.           {
  1967.         search_start++;
  1968.           }
  1969.         else
  1970.           {
  1971.         /* Initialize the start of a new search. */
  1972.         if (starting_filename)
  1973.           free (starting_filename);
  1974.  
  1975.         starting_filename = savestring (last_loaded_info_file);
  1976.  
  1977.         if (starting_nodename)
  1978.           free (starting_nodename);
  1979.  
  1980.         starting_nodename = savestring (nodename ? nodename : "");
  1981.  
  1982.         starting_pagetop = pagetop;
  1983.         search_start = pagetop;
  1984.         wrap_search = false;
  1985.  
  1986.         if (*nodename != '\0')
  1987.           strcpy (search_string, nodename);
  1988.           }
  1989.  
  1990.         I_goto_xy (echo_area.left, echo_area.top);
  1991.  
  1992.         {
  1993.           static int pushed = 0; /* how many files are pushed? */
  1994.           boolean found_string = false; /* did we find our string? */
  1995.  
  1996.           if (wrap_search)
  1997.         {
  1998.           push_filestack (next_info_file ((char *)NULL, true), false);
  1999.           pushed++;
  2000.           search_start = 0;
  2001.         }
  2002.  
  2003.           for (;;)
  2004.         {
  2005.           set_search_constraints (info_file, info_buffer_len);
  2006.           pointer = search_forward (search_string, search_start);
  2007.  
  2008.           if (pointer != -1)
  2009.             {
  2010.               found_string = true;
  2011.               break;
  2012.             }
  2013.           else
  2014.             {
  2015.               char *next_file;
  2016.  
  2017.               next_file = next_info_file ((char *)NULL, false);
  2018.  
  2019.               if (next_file != NULL)
  2020.             {
  2021.               if (pushed)
  2022.                 {
  2023.                   pop_filestack ();
  2024.                   pushed--;
  2025.                 }
  2026.  
  2027.               push_filestack (next_file, false);
  2028.               pushed++;
  2029.               search_start = 0;
  2030. #if 1
  2031.               I_goto_xy (echo_area.left, echo_area.top);
  2032.               print_string ("Searching file %s...\n", next_file);
  2033. #endif
  2034.               continue;
  2035.             }
  2036.  
  2037.               if (wrap_search)
  2038.             {
  2039.               display_error
  2040.                 ("\"%s\" not found!",
  2041.                  search_string);
  2042.  
  2043.               inhibit_display = true;
  2044.               wrap_search = false;
  2045.  
  2046.               if (pushed)
  2047.                 {
  2048.                   pop_filestack ();
  2049.                   pushed--;
  2050.                 }
  2051.               break;
  2052.             }
  2053.               else
  2054.             {
  2055.               display_error ("Search: End of file");
  2056.               inhibit_display = true;
  2057.               wrap_search = true;
  2058.  
  2059.               if (pushed)
  2060.                 {
  2061.                   pop_filestack ();
  2062.                   pushed--;
  2063.                 }
  2064.               break;
  2065.             }
  2066.             }
  2067.         }
  2068.  
  2069.           if (pushed)
  2070.         {
  2071.           swap_filestack ();
  2072.           pop_filestack ();
  2073.           pushed--;
  2074.         }
  2075.  
  2076.           if (!found_string)
  2077.         break;
  2078.  
  2079.           wrap_search = false;
  2080.           temp = search_backward (start_of_node_string, pointer);
  2081.  
  2082.           if (temp == -1)
  2083.         temp = search_forward (start_of_node_string, pointer);
  2084.  
  2085.           if (temp == -1)
  2086.         {
  2087.           brians_error ();
  2088.           break;
  2089.         }
  2090.          
  2091.           search_start = pointer;
  2092.           pointer = forward_lines (1, temp);
  2093.  
  2094.           if (!extract_field ("Node:", nodename, pointer))
  2095.         {
  2096.           display_error
  2097.             ("There doesn't appear to be a nodename for this node.");
  2098.           get_node ((char *)NULL, "*", false);
  2099.           pagetop = pointer;
  2100.           break;
  2101.         }
  2102.          
  2103.           /* Get the node if it is different than the one already
  2104.          loaded. */
  2105.           if (strcmp (nodename, starting_nodename) != 0)
  2106.         {
  2107.           free (starting_nodename);
  2108.           starting_nodename = savestring (nodename);
  2109.  
  2110.           if (get_node ((char *) NULL, nodename, false))
  2111.             clear_echo_area ();
  2112.         }
  2113.          
  2114.           /* Reset the top of page if necessary. */
  2115.           {
  2116.         if ((strcmp (last_loaded_info_file, starting_filename) != 0) ||
  2117.             (starting_pagetop != pagetop) ||
  2118.             (search_start > pagebot))
  2119.           {
  2120.             pointer =
  2121.               back_lines ((the_window.bottom - the_window.top) / 2,
  2122.                   forward_lines (1, search_start));
  2123.  
  2124.             if (pointer < nodetop)
  2125.               pointer = nodetop;
  2126.            
  2127.             pagetop = pointer;
  2128.             window_bashed = true;
  2129.           }
  2130.         else
  2131.           inhibit_display = true;
  2132.           }
  2133.           break;
  2134.         }
  2135.          }
  2136.  
  2137.     case CTRL ('H'):
  2138.     case '?':
  2139.       help_use_info ();
  2140.       last_pagetop = -1;
  2141.       break;
  2142.  
  2143.     case 'Q':
  2144.       done = true;
  2145.       break;
  2146.  
  2147.     case CTRL ('L'):    /* Control-l is redisplay. */
  2148.       window_bashed = true;
  2149.       if (last_command == 'S')
  2150.         command = 'S';
  2151.       break;
  2152.  
  2153.     case '(':    /* You *must* be trying to type a complete nodename. */
  2154.       strcpy (nodename, "(");
  2155.       if (!readline ("Goto node: ", nodename, NODENAME_LEN, false))
  2156.         {
  2157.           inhibit_display = true;
  2158.           clear_echo_area ();
  2159.           break;
  2160.         }
  2161.       I_goto_xy (echo_area.left, echo_area.top);
  2162.       if (get_node ((char *) NULL, nodename, false))
  2163.         clear_echo_area ();
  2164.       break;
  2165.  
  2166.     case CTRL ('P'):
  2167.       /* Print the contents of this node on the default printer.  We
  2168.          would like to let the user specify the printer, but we don't
  2169.          want to ask her each time which printer to use.  Besides, he
  2170.          might not know, which is why it (the user) is in the need of
  2171.          Info. */
  2172.       {
  2173.         char *tempname = make_temp_filename (current_info_node);
  2174.         if (dump_current_node (tempname) == 0 &&
  2175.         printfile (tempname) == 0 &&
  2176.         deletefile (tempname) == 0)
  2177.           {
  2178.         display_error ("Printed node.  Go pick up your output.\n");
  2179.           }
  2180.         inhibit_display = true;
  2181.         free (tempname);
  2182.       }
  2183.       break;
  2184.  
  2185.     default:
  2186.       inhibit_display = true;
  2187.       display_error ("Unknown command! Press '?' for help.");
  2188.  
  2189.     }
  2190.     }
  2191. }
  2192.  
  2193. /* Return the screen column width that the line from START to END
  2194.    requires to display. */
  2195. line_length (start, end)
  2196.      int start, end;
  2197. {
  2198.   int count = 0;
  2199.  
  2200.   while (start < end)
  2201.     {
  2202.       if (info_file[start] == '\t')
  2203.     count += 7 - (count % 8);
  2204.       else if (CTRL_P (info_file[start]))
  2205.     count += 2;
  2206.       else
  2207.     count++;
  2208.  
  2209.       start++;
  2210.     }
  2211.  
  2212.   return (count);
  2213. }
  2214.  
  2215. /* Tell this person how to use Info. */
  2216. help_use_info ()
  2217. {
  2218.   open_typeout ();
  2219.   clear_screen ();
  2220.   print_string ("\n\
  2221.           Commands in Info\n\
  2222. \n\
  2223. h    Invoke the Info tutorial.\n\
  2224. \n\
  2225. Selecting other nodes:\n\
  2226. n    Move to the \"next\" node of this node.\n\
  2227. p    Move to the \"previous\" node of this node.\n\
  2228. u    Move \"up\" from this node.\n\
  2229. m    Pick menu item specified by name.\n\
  2230.     Picking a menu item causes another node to be selected.\n\
  2231. f    Follow a cross reference.  Reads name of reference.\n\
  2232. l    Move to the last node you were at.\n\
  2233. \n\
  2234. Moving within a node:\n\
  2235. Space    Scroll forward a page.\n\
  2236. DEL    Scroll backward a page.\n\
  2237. b    Go to the beginning of this node.\n\
  2238. \n\
  2239. Advanced commands:\n\
  2240. q    Quit Info.\n\
  2241. 1    Pick first item in node's menu.\n\
  2242. 2 - 5   Pick second ... fifth item in node's menu.\n\
  2243. g    Move to node specified by name.\n\
  2244.     You may include a filename as well, as (FILENAME)NODENAME.\n\
  2245. s    Search through this Info file for a specified string,\n\
  2246.     and select the node in which the next occurrence is found.\n\
  2247. Ctl-p   Print the contents of this node using `%s'.\n\
  2248. \n\
  2249. Done.\n\n",print_command);
  2250.   close_typeout ();
  2251. }
  2252.  
  2253. /* Move to the node specified in the NEXT field. */
  2254. boolean
  2255. next_node ()
  2256. {
  2257.   char nodename[NODENAME_LEN];
  2258.  
  2259.   if (!extract_field ("Next:", nodename, nodetop))
  2260.     return (false);
  2261.   return (get_node ((char *) NULL, nodename, false));
  2262. }
  2263.  
  2264. /* Move to the node specified in the PREVIOUS field. */
  2265. boolean
  2266. prev_node ()
  2267. {
  2268.   char nodename[NODENAME_LEN];
  2269.  
  2270.   if (!extract_field ("Previous:", nodename, nodetop)
  2271.       && !extract_field ("Prev:", nodename, nodetop))
  2272.     return (false);
  2273.   return (get_node ((char *) NULL, nodename, false));
  2274. }
  2275.  
  2276. /* Move to the node specified in the UP field. */
  2277. boolean
  2278. up_node ()
  2279. {
  2280.   char nodename[NODENAME_LEN];
  2281.  
  2282.   if (!extract_field ("Up:", nodename, nodetop))
  2283.     return (false);
  2284.   return (get_node ((char *) NULL, nodename, false));
  2285. }
  2286.  
  2287. /* Build a completion list of menuname/nodename for each
  2288.    line in this node that is a menu item. */
  2289. boolean
  2290. build_menu ()
  2291. {
  2292.   int pointer = nodetop;
  2293.   char menuname[NODENAME_LEN];
  2294.   char nodename[NODENAME_LEN];
  2295.  
  2296.   if (strcmp (menus_nodename, current_info_node) == 0 &&
  2297.       strcmp (menus_filename, current_info_file) == 0)
  2298.     return (the_menu_size != 0);
  2299.  
  2300.   strcpy (menus_nodename, current_info_node);
  2301.   strcpy (menus_filename, current_info_file);
  2302.   free_completion_list ();
  2303.   the_menu_size = 0;
  2304.  
  2305.   set_search_constraints (info_file, nodebot);
  2306.   if ((pointer = search_forward (MENU_HEADER, nodetop)) < 0)
  2307.     return (false);
  2308.  
  2309.   /* There is a menu here.  Look for members of it. */
  2310.   pointer += strlen (MENU_HEADER);
  2311.  
  2312.   while (true)
  2313.     {
  2314.       int idx;
  2315.  
  2316.       pointer = search_forward (MENU_ID, pointer);
  2317.       if (pointer < 0)
  2318.     break;            /* No more menus in this node. */
  2319.  
  2320.       pointer = (skip_whitespace (pointer + strlen (MENU_ID)));
  2321.  
  2322.       idx = 0;
  2323.       while ((menuname[idx] = info_file[pointer]) && menuname[idx] != ':')
  2324.     idx++, pointer++;
  2325.  
  2326.       menuname[idx] = '\0';
  2327.       pointer++;
  2328.  
  2329.       if (info_file[pointer] == ':')
  2330.     {
  2331.       strcpy (nodename, menuname);
  2332.     }
  2333.       else
  2334.     {
  2335.       pointer = skip_whitespace (pointer);
  2336.       idx = 0;
  2337.       while ((nodename[idx] = info_file[pointer]) &&
  2338.          nodename[idx] != '\t' &&
  2339.          nodename[idx] != '.' &&
  2340.          nodename[idx] != ',')
  2341.         {
  2342.           idx++, pointer++;
  2343.         }
  2344.       nodename[idx] = '\0';
  2345.     }
  2346.       add_completion (menuname, nodename);
  2347.       the_menu_size++;
  2348.     }
  2349.   if (the_menu_size)
  2350.     completion_list = reverse_list (completion_list);
  2351.   return (the_menu_size != 0);
  2352. }
  2353.  
  2354. /* Select ITEMth item from a list built by build_menu (). */
  2355. boolean
  2356. get_menu (item)
  2357.      int item;
  2358. {
  2359.   if (!build_menu ())
  2360.     return (false);
  2361.  
  2362.   if (item > the_menu_size)
  2363.     return (false);
  2364.   else
  2365.     {
  2366.       COMP_ENTRY *temp = completion_list;
  2367.  
  2368.       while (--item && temp)
  2369.     temp = temp->next;
  2370.  
  2371.       return (get_node ((char *) NULL, temp->data, false));
  2372.     }
  2373. }
  2374.  
  2375. /* Scan through the ?already? built menu list looking
  2376.    for STRING.  If you find it, put the corresponding nodes
  2377.    name in NODENAME. */
  2378. boolean
  2379. find_menu_node (string, nodename)
  2380.      char *string, *nodename;
  2381. {
  2382.   return (scan_list (string, nodename));
  2383. }
  2384.  
  2385. /* The work part of find_menu_node and find_note_node. */
  2386. boolean
  2387. scan_list (string, nodename)
  2388.      char *string, *nodename;
  2389. {
  2390.   COMP_ENTRY *temp = completion_list;
  2391.  
  2392.   while (temp)
  2393.     {
  2394.       if (stricmp (string, temp->identifier, strlen (string)) == 0)
  2395.     {
  2396.       strcpy (nodename, temp->data);
  2397.       return (true);
  2398.     }
  2399.       temp = temp->next;
  2400.     }
  2401.   return (false);
  2402. }
  2403.  
  2404. /* Remove <CR> and whitespace from string, replacing them with
  2405.    only one space.  Exception:  <CR> at end of string disappears. */
  2406. clean_up (string)
  2407.      char *string;
  2408. {
  2409.   char *to = string;
  2410.  
  2411.   while (*to = *string++)
  2412.     {
  2413.       if (*to == '\n' || *to == ' ')
  2414.     {
  2415.       *to = ' ';
  2416.  
  2417.       while (*string == ' ' || *string == '\t')
  2418.         string++;
  2419.     }
  2420.     to++;
  2421.     }
  2422. }
  2423.  
  2424. /* Find a reference to "*Note".  Return the offset of the start
  2425.    of that reference, or -1. */
  2426. find_footnote_ref (from)
  2427.      int from;
  2428. {
  2429.   while (true)
  2430.     {
  2431.       from = search_forward (FOOTNOTE_HEADER, from);
  2432.       if (from < 0)
  2433.     return (from);
  2434.       else
  2435.     from += strlen (FOOTNOTE_HEADER);
  2436.       if (info_file[from] == ' ' ||
  2437.       info_file[from] == '\n' ||
  2438.       info_file[from] == '\t')
  2439.     return (from);
  2440.     }
  2441. }
  2442.  
  2443. /* Build an array of (footnote.nodename) for each footnote in this node. */
  2444. boolean
  2445. build_notes ()
  2446. {
  2447.   int pointer;
  2448.   char notename[NODENAME_LEN];
  2449.   char nodename[NODENAME_LEN];
  2450.  
  2451.   set_search_constraints (info_file, nodebot);
  2452.  
  2453.   if ((find_footnote_ref (nodetop)) < 0)
  2454.     return (false);
  2455.   pointer = nodetop;
  2456.  
  2457.   menus_filename[0] = menus_nodename[0] = '\0';
  2458.   visible_footnote = "";
  2459.   free_completion_list ();
  2460.  
  2461.   while (true)
  2462.     {
  2463.       int idx;
  2464.  
  2465.       pointer = find_footnote_ref (pointer);
  2466.       if (pointer < 0)
  2467.     break;            /* no more footnotes in this node. */
  2468.  
  2469.       pointer = skip_whitespace_and_cr (pointer);
  2470.       idx = 0;
  2471.  
  2472.       while ((notename[idx] = info_file[pointer]) && notename[idx] != ':')
  2473.     {
  2474.       idx++, pointer++;
  2475.     }
  2476.  
  2477.       notename[idx] = '\0';
  2478.       clean_up (notename);
  2479.       pointer++;
  2480.       if (info_file[pointer] == ':')
  2481.     {
  2482.       strcpy (nodename, notename);
  2483.     }
  2484.       else
  2485.     {
  2486.       int in_parens = 0;
  2487.  
  2488.       pointer = skip_whitespace (pointer);
  2489.       idx = 0;
  2490.  
  2491.       while ((nodename[idx] = info_file[pointer]) &&
  2492.          (in_parens ||
  2493.           (nodename[idx] != '\t' &&
  2494.            nodename[idx] != '.' &&
  2495.            nodename[idx] != ',')))
  2496.         {
  2497.           if (nodename[idx] == '(')
  2498.         in_parens++;
  2499.           else if (nodename[idx] == ')')
  2500.         in_parens--;
  2501.  
  2502.           idx++, pointer++;
  2503.         }
  2504.       nodename[idx] = '\0';
  2505.       clean_up (nodename);
  2506.     }
  2507.       /* Add the notename/nodename to the list. */
  2508.       add_completion (notename, nodename);
  2509.       the_menu_size++;
  2510.  
  2511.       /* Remember this identifier as the default if it is the first one in the
  2512.          page. */
  2513.       if (!(*visible_footnote) &&
  2514.       pointer > pagetop &&
  2515.       pointer < forward_lines (the_window.bottom - the_window.top, pointer))
  2516.     visible_footnote = completion_list->identifier;
  2517.     }
  2518.   if (the_menu_size)
  2519.     completion_list = reverse_list (completion_list);
  2520.   return (the_menu_size != 0);
  2521. }
  2522.  
  2523. /* Scan through the ?already? built footnote list looking
  2524.    for STRING.  If found, place the corresponding node name
  2525.    in NODENAME. */
  2526. boolean
  2527. find_note_node (string, nodename)
  2528.      char *string, *nodename;
  2529. {
  2530.   return (scan_list (string, nodename));
  2531. }
  2532.  
  2533. /* **************************************************************** */
  2534. /*                                    */
  2535. /*            Page Display                     */
  2536. /*                                    */
  2537. /* **************************************************************** */
  2538.  
  2539.  
  2540. /* The display functions for GNU Info. */
  2541. int display_ch, display_cv;
  2542. int display_point;
  2543.  
  2544. /* Display the current page from pagetop down to the bottom of the
  2545.    page or the bottom of the node, whichever comes first. */
  2546. display_page ()
  2547. {
  2548.   display_point = pagetop;
  2549.   display_ch = the_window.left;
  2550.   display_cv = the_window.top;
  2551.   generic_page_display ();
  2552. }
  2553.  
  2554. /* Print the page from display_point to bottom of node, or window,
  2555.    whichever comes first.  Start printing at display_ch, display_cv. */
  2556. generic_page_display ()
  2557. {
  2558.   int done_with_display = 0;
  2559.   int character;
  2560.  
  2561.   goto_xy (display_ch, display_cv);
  2562.  
  2563.   while (!done_with_display)
  2564.     {
  2565.       if (display_point == nodebot)
  2566.     {
  2567.       clear_eop ();
  2568.       goto display_finish;
  2569.     }
  2570.  
  2571.       character = info_file[display_point];
  2572.  
  2573.       if ((display_width (character, the_window.ch) + the_window.ch)
  2574.       >= the_window.right)
  2575.     display_carefully (character);
  2576.       else
  2577.     charout (character);
  2578.  
  2579.       if ((the_window.cv >= the_window.bottom)
  2580.       || (the_window.cv == the_window.top
  2581.           && the_window.ch == the_window.left))
  2582.     {
  2583.     display_finish:
  2584.       pagebot = display_point;
  2585.       make_modeline ();
  2586.       done_with_display++;
  2587.       continue;
  2588.     }
  2589.       else
  2590.     display_point++;
  2591.     }
  2592.   fflush (stdout);
  2593. }
  2594.  
  2595. /* Display character carefully, ensuring that no scrolling takes
  2596.    place, even in the case of funky control characters. */
  2597. display_carefully (character)
  2598.      int character;
  2599. {
  2600.   if (CTRL_P (character))
  2601.     {
  2602.       switch (character)
  2603.     {
  2604.     case RETURN:
  2605.     case NEWLINE:
  2606.     case TAB:
  2607.       clear_eol ();
  2608.       advance (the_window.right - the_window.ch);
  2609.       break;
  2610.     default:
  2611.       charout ('^');
  2612.       if (the_window.cv == the_window.bottom)
  2613.         break;
  2614.       else
  2615.         charout (UNCTRL (character));
  2616.     }
  2617.     }
  2618.   else
  2619.     charout (character);
  2620. }
  2621.  
  2622. /* Move the cursor to POSITION in page.  Return non-zero if successful. */
  2623. cursor_to (position)
  2624.      int position;
  2625. {
  2626.   int ch, cv, character;
  2627.   int point;
  2628.  
  2629.   if (position > pagebot || position < pagetop)
  2630.     return (0);
  2631.  
  2632.   point = pagetop;
  2633.   ch = the_window.left;
  2634.   cv = the_window.top;
  2635.  
  2636.   while (point < position)
  2637.     {
  2638.       character = info_file[point++];
  2639.  
  2640.       ch += display_width (character, ch);
  2641.  
  2642.       if (ch >= the_window.right)
  2643.     {
  2644.       ch = ch - the_window.right;
  2645.       cv++;
  2646.  
  2647.       if (cv >= the_window.bottom)
  2648.         return (0);
  2649.     }
  2650.     }
  2651.   goto_xy (ch, cv);
  2652.   return (1);
  2653. }
  2654.  
  2655. /* Move to the next page in this node.  Return FALSE if
  2656.    we can't get to the next page. */
  2657. boolean
  2658. next_page ()
  2659. {
  2660.   int pointer;
  2661.  
  2662.   pointer =
  2663.     forward_lines ((the_window.bottom - the_window.top) - 2, pagetop);
  2664.  
  2665.   if (pointer >= nodebot)
  2666.     return (false);
  2667.  
  2668.   /* Hack for screens smaller than displayed line width. */
  2669.   if (pointer > display_point)
  2670.     {
  2671.       pointer = display_point;
  2672.       back_lines (1);
  2673.     }
  2674.   pagetop = pointer;
  2675.   return (true);
  2676. }
  2677.  
  2678. /* Move to the previous page in this node.  Return FALSE if
  2679.    there is no previous page. */
  2680. boolean
  2681. prev_page ()
  2682. {
  2683.   int pointer =
  2684.   back_lines ((the_window.bottom - the_window.top) - 2, pagetop);
  2685.  
  2686.   if (pagetop == nodetop)
  2687.     return (false);
  2688.  
  2689.   if (pointer < nodetop)
  2690.     pointer = nodetop;
  2691.  
  2692.   pagetop = pointer;
  2693.   return (true);
  2694. }
  2695.  
  2696.  
  2697. /* **************************************************************** */
  2698. /*                                    */
  2699. /*            Utility Functions                */
  2700. /*                                    */
  2701. /* **************************************************************** */
  2702.  
  2703. char *search_buffer;        /* area in ram to scan through. */
  2704. int buffer_bottom;        /* Length of this area. */
  2705.  
  2706. /* Set the global variables that all of these routines use. */
  2707. set_search_constraints (buffer, extent)
  2708.      char *buffer;
  2709.      int extent;
  2710. {
  2711.   search_buffer = buffer;
  2712.   buffer_bottom = extent;
  2713. }
  2714.  
  2715. /* Move back to the start of this line. */
  2716. to_beg_line (from)
  2717.      int from;
  2718. {
  2719.   while (from && search_buffer[from - 1] != '\n')
  2720.     from--;
  2721.   return (from);
  2722. }
  2723.  
  2724. /* Move forward to the end of this line. */
  2725. to_end_line (from)
  2726. {
  2727.   while (from < buffer_bottom && search_buffer[from] != '\n')
  2728.     from++;
  2729.   return (from);
  2730. }
  2731.  
  2732. /* Move back count lines in search_buffer starting at starting_pos.
  2733.    Returns the start of that line. */
  2734. back_lines (count, starting_pos)
  2735.      int count, starting_pos;
  2736. {
  2737.   starting_pos = to_beg_line (starting_pos);
  2738.   while (starting_pos && count)
  2739.     {
  2740.       starting_pos = to_beg_line (starting_pos - 1);
  2741.       count--;
  2742.     }
  2743.   return (starting_pos);
  2744. }
  2745.  
  2746. /* Move forward count lines starting at starting_pos.
  2747.    Returns the start of that line. */
  2748. forward_lines (count, starting_pos)
  2749.      int count, starting_pos;
  2750. {
  2751.   starting_pos = to_end_line (starting_pos);
  2752.   while (starting_pos < buffer_bottom && count)
  2753.     {
  2754.       starting_pos = to_end_line (starting_pos + 1);
  2755.       count--;
  2756.     }
  2757.   return (to_beg_line (starting_pos));
  2758. }
  2759.  
  2760. /* Search for STRING in SEARCH_BUFFER starting at STARTING_POS.
  2761.    Return the location of the string, or -1 if not found. */
  2762. search_forward (string, starting_pos)
  2763.      char *string;
  2764.      int starting_pos;
  2765. {
  2766.   register int c, i, len;
  2767.   register char *buff, *end;
  2768.   char *alternate;
  2769.  
  2770.  
  2771.   /* We match characters in SEARCH_BUFFER against STRING and ALTERNATE.
  2772.      ALTERNATE is a case reversed version of STRING; this is cheaper than
  2773.      case folding each character before comparison. */
  2774.  
  2775.   /* Build the alternate string. */
  2776.   alternate = savestring (string);
  2777.   len = strlen (string);
  2778.  
  2779.   for (i = 0; i < len; i++)
  2780.     {
  2781.       c = alternate[i];
  2782.  
  2783.       if (c >= 'a' && c <= 'z')
  2784.     alternate[i] = c - 32;
  2785.       else if (c >= 'A' && c <= 'Z')
  2786.     alternate[i] = c + 32;
  2787.     }
  2788.  
  2789.   buff = search_buffer + starting_pos;
  2790.   end = search_buffer + buffer_bottom + 1;
  2791.  
  2792.   while (buff < end)
  2793.     {
  2794.       for (i = 0; i < len; i++)
  2795.     {
  2796.       c = buff[i];
  2797.  
  2798.       if (c != string[i] && c != alternate[i])
  2799.         break;
  2800.     }
  2801.  
  2802.       if (!string[i])
  2803.     {
  2804.       free (alternate);
  2805.       return (buff - search_buffer);
  2806.     }
  2807.  
  2808.       buff++;
  2809.     }
  2810.  
  2811.   free (alternate);
  2812.   return (-1);
  2813. }
  2814.  
  2815. /* Search for STRING in SEARCH_BUFFER starting at STARTING_POS.
  2816.    Return the location of the string, or -1 if not found. */
  2817. search_backward (string, starting_pos)
  2818.      char *string;
  2819.      int starting_pos;
  2820. {
  2821.   int len = strlen (string);
  2822.   while (starting_pos - len > -1)
  2823.     {
  2824.       if (strnicmp (search_buffer + (starting_pos - len), string, len) == 0)
  2825.     return (starting_pos - len);
  2826.       else
  2827.     starting_pos--;
  2828.     }
  2829.   return (-1);
  2830. }
  2831.  
  2832. /* Only search for STRING from POINTER to end of line.  Return offset
  2833.    of string, or -1 if not found. */
  2834. string_in_line (string, pointer)
  2835.      char *string;
  2836.      int pointer;
  2837. {
  2838.   int old_buffer_bottom = buffer_bottom;
  2839.  
  2840.   set_search_constraints (search_buffer, to_end_line (pointer));
  2841.   pointer = search_forward (string, pointer);
  2842.   buffer_bottom = old_buffer_bottom;
  2843.   return (pointer);
  2844. }
  2845.  
  2846. /* Skip whitespace characters at OFFSET in SEARCH_BUFFER.
  2847.    Return the next non-whitespace character or -1 if BUFFER_BOTTOM
  2848.    is reached. */
  2849. skip_whitespace (offset)
  2850.      int offset;
  2851. {
  2852.   int character;
  2853.  
  2854.   while (offset < buffer_bottom)
  2855.     {
  2856.       character = search_buffer[offset];
  2857.       if (character == ' ' || character == '\t')
  2858.     offset++;
  2859.       else
  2860.     return (offset);
  2861.     }
  2862.   return (-1);
  2863. }
  2864.  
  2865. /* Skip whitespace characters including <CR> at OFFSET in
  2866.    SEARCH_BUFFER.  Return the position of the next non-whitespace
  2867.    character, or -1 if BUFFER_BOTTOM is reached. */
  2868. skip_whitespace_and_cr (offset)
  2869.      int offset;
  2870. {
  2871.   while (true)
  2872.     {
  2873.       offset = skip_whitespace (offset);
  2874.       if (offset > 0 && search_buffer[offset] != '\n')
  2875.     return (offset);
  2876.       else
  2877.     offset++;
  2878.     }
  2879. }
  2880.  
  2881. /* Extract the node name part of the of the text after the FIELD.
  2882.    Place the node name into NODENAME.  Assume the line starts at
  2883.    OFFSET in SEARCH_BUFFER. */
  2884. boolean
  2885. extract_field (field_name, nodename, offset)
  2886.      char *field_name, *nodename;
  2887.      int offset;
  2888. {
  2889.   int temp, character;
  2890.  
  2891.   temp = string_in_line (field_name, offset);
  2892.   if (temp < 0)
  2893.     return (false);
  2894.  
  2895.   temp += strlen (field_name);
  2896.   temp = skip_whitespace (temp);
  2897.  
  2898.   /* Okay, place the following text into NODENAME. */
  2899.  
  2900.   while ((character = search_buffer[temp]) != ','
  2901.      && character != '\n'
  2902.      && character != '\t')
  2903.     {
  2904.       *nodename = character;
  2905.       nodename++;
  2906.       temp++;
  2907.     }
  2908.   *nodename = '\0';
  2909.   return (true);
  2910. }
  2911.  
  2912. /* Return true if pointer is exactly at string, else false. */
  2913. boolean
  2914. looking_at (string, pointer)
  2915.      char *string;
  2916.      int pointer;
  2917. {
  2918.   if (strnicmp (search_buffer + pointer, string, strlen (string)) == 0)
  2919.     return (true);
  2920.   else
  2921.     return (false);
  2922. }
  2923.  
  2924. /* File stack stuff. This is currently only used to push one file while
  2925.    searching indirect files, but we may as well write it in full
  2926.    generality. */
  2927. typedef struct filestack
  2928. {
  2929.   struct filestack *next;
  2930.   char filename[FILENAME_LEN];
  2931.   char current_filename[FILENAME_LEN];
  2932.   char *tag_table;
  2933.   char *info_file;
  2934.   int info_buffer_len;
  2935. } FILESTACK;
  2936.  
  2937. FILESTACK *filestack = NULL;
  2938.  
  2939. boolean
  2940. push_filestack (filename, remember_name)
  2941.      char *filename;
  2942.      int remember_name;
  2943. {
  2944.   FILESTACK *element = (FILESTACK *)xmalloc (sizeof (FILESTACK));
  2945.  
  2946.   element->next = filestack;
  2947.   filestack = element;
  2948.  
  2949.   strcpy (filestack->filename, last_loaded_info_file);
  2950.   strcpy (filestack->current_filename, current_info_file);
  2951.   filestack->tag_table = tag_table;
  2952.   filestack->info_file = info_file;
  2953.   filestack->info_buffer_len = info_buffer_len;
  2954.   
  2955.   *last_loaded_info_file = '\0';    /* force the file to be read */
  2956.   if (get_info_file (filename, remember_name))
  2957.     {
  2958.       return (true);
  2959.     }
  2960.   else
  2961.     {
  2962.       pop_filestack ();
  2963.       return (false);
  2964.     }
  2965. }
  2966.  
  2967. void
  2968. pop_filestack ()
  2969. {
  2970.   FILESTACK *temp;
  2971.    
  2972.   if (filestack == NULL)
  2973.     {
  2974.        fprintf (stderr , "File stack is empty and can't be popped\n");
  2975.        brians_error ();
  2976.        return;
  2977.     }
  2978.  
  2979.   free (info_file);
  2980.  
  2981.   strcpy (last_loaded_info_file, filestack->filename);
  2982.   strcpy (current_info_file, filestack->current_filename);
  2983.   tag_table = filestack->tag_table;
  2984.   info_file = filestack->info_file;
  2985.   info_buffer_len = filestack->info_buffer_len;
  2986.  
  2987.   temp = filestack;
  2988.   filestack = filestack->next;
  2989.   free ((char *)temp);
  2990. }
  2991.  
  2992. /* Swap the current info file with the bottom of the filestack */
  2993. void
  2994. swap_filestack ()
  2995. {
  2996.   char t_last_loaded_info_file[FILENAME_LEN];
  2997.   char t_current_info_file[FILENAME_LEN];
  2998.   char *t_tag_table;
  2999.   char *t_info_file;
  3000.   int t_info_buffer_len;
  3001.  
  3002.   if (filestack == NULL)
  3003.     {
  3004.        fprintf (stderr , "File stack is empty and can't be swapped\n");
  3005.        brians_error ();
  3006.        return;
  3007.     }
  3008.  
  3009.   strcpy (t_last_loaded_info_file, filestack->filename);
  3010.   strcpy (t_current_info_file, filestack->current_filename);
  3011.   t_tag_table = filestack->tag_table;
  3012.   t_info_file = info_file;
  3013.   t_info_buffer_len = info_buffer_len;
  3014.  
  3015.   strcpy (filestack->filename, last_loaded_info_file);
  3016.   strcpy (filestack->current_filename, current_info_file);
  3017.   filestack->tag_table = tag_table;
  3018.   filestack->info_file = info_file;
  3019.   filestack->info_buffer_len = info_buffer_len;
  3020.  
  3021.   strcpy (last_loaded_info_file, t_last_loaded_info_file);
  3022.   strcpy (current_info_file, t_current_info_file);
  3023.   tag_table = t_tag_table;
  3024.   info_file = t_info_file;
  3025.   info_buffer_len = t_info_buffer_len;
  3026. }
  3027.  
  3028. /* Now the node history stack */
  3029.  
  3030. extern NODEINFO *Info_History;
  3031.  
  3032. /* Save the current filename, nodename, and position on the history list.
  3033.    We prepend. */
  3034. boolean
  3035. push_node (filename, nodename, page_position, node_position)
  3036.      char *filename, *nodename;
  3037.      int page_position, node_position;
  3038. {
  3039.   NODEINFO *newnode = (NODEINFO *) xmalloc (sizeof (NODEINFO));
  3040.  
  3041.   newnode->next = Info_History;
  3042.  
  3043.   newnode->filename = (char *) xmalloc (strlen (filename) + 1);
  3044.   strcpy (newnode->filename, filename);
  3045.  
  3046.   newnode->nodename = (char *) xmalloc (strlen (nodename) + 1);
  3047.   strcpy (newnode->nodename, nodename);
  3048.  
  3049.   newnode->pagetop = page_position;
  3050.   newnode->nodetop = node_position;
  3051.  
  3052.   Info_History = newnode;
  3053.   return (true);
  3054. }
  3055.  
  3056. /* Pop one node from the node list, leaving the values in
  3057.    passed variables. */
  3058. boolean
  3059. pop_node (filename, nodename, nodetop, pagetop)
  3060.      char *filename, *nodename;
  3061.      int *nodetop, *pagetop;
  3062. {
  3063.   if (Info_History->next == (NODEINFO *) NULL)
  3064.     {
  3065.       display_error ("At beginning of history now!");
  3066.       return (false);
  3067.     }
  3068.   else
  3069.     {
  3070.       NODEINFO *releaser = Info_History;
  3071.  
  3072.     /* If the popped file is not the current file, then force
  3073.        the popped file to be loaded. */
  3074.       if (strcmp (Info_History->filename, last_loaded_info_file) != 0)
  3075.     last_loaded_info_file[0] = '\0';
  3076.  
  3077.       strcpy (filename, Info_History->filename);
  3078.       strcpy (nodename, Info_History->nodename);
  3079.       *pagetop = Info_History->pagetop;
  3080.       *nodetop = Info_History->nodetop;
  3081.       free (Info_History->nodename);
  3082.       free (Info_History->filename);
  3083.       Info_History = Info_History->next;
  3084.       free (releaser);
  3085.       return (true);
  3086.     }
  3087. }
  3088.  
  3089. /* Whoops, Unix doesn't have strnicmp. */
  3090.  
  3091. /* Compare at most COUNT characters from string1 to string2.  Case
  3092.    doesn't matter. */
  3093. int
  3094. strnicmp (string1, string2, count)
  3095.      char *string1, *string2;
  3096. {
  3097.   char ch1, ch2;
  3098.  
  3099.   while (count)
  3100.     {
  3101.       ch1 = *string1++;
  3102.       ch2 = *string2++;
  3103.       if (to_upper (ch1) == to_upper (ch2))
  3104.     count--;
  3105.       else
  3106.     break;
  3107.     }
  3108.   return (count);
  3109. }
  3110.  
  3111. /* Compare string1 to string2.  Case doesn't matter. */
  3112. int
  3113. stricmp (string1, string2)
  3114.      char *string1, *string2;
  3115. {
  3116.   char ch1, ch2;
  3117.  
  3118.   while (1)
  3119.     {
  3120.       ch1 = *string1++;
  3121.       ch2 = *string2++;
  3122.  
  3123.       if (ch1 == '\0')
  3124.     return (ch2 != '\0');
  3125.  
  3126.       if ((ch2 == '\0') ||
  3127.       (to_upper (ch1) != to_upper (ch2)))
  3128.     return (1);
  3129.     }
  3130. }
  3131.  
  3132. /* Make the user type "Y" or "N". */
  3133. boolean 
  3134. get_y_or_n_p ()
  3135. {
  3136.   int character;
  3137.   print_string (" (Y or N)?");
  3138.   clear_eol ();
  3139.  
  3140. until_we_like_it:
  3141.  
  3142.   character = blink_cursor ();
  3143.   if (character == EOF)
  3144.     return (false);
  3145.   if (to_upper (character) == 'Y')
  3146.     {
  3147.       charout (character);
  3148.       return (true);
  3149.     }
  3150.  
  3151.   if (to_upper (character) == 'N')
  3152.     {
  3153.       charout (character);
  3154.       return (false);
  3155.     }
  3156.  
  3157.   if (character == ABORT_CHAR)
  3158.     {
  3159.       ding ();
  3160.       return (false);
  3161.     }
  3162.  
  3163.   goto until_we_like_it;
  3164. }
  3165.  
  3166. /* Move the cursor to the desired column in the window. */
  3167. indent_to (screen_column)
  3168.      int screen_column;
  3169. {
  3170.   int counter = screen_column - the_window.ch;
  3171.   if (counter > 0)
  3172.     {
  3173.       while (counter--)
  3174.     charout (' ');
  3175.     }
  3176.   else if (screen_column != 0)
  3177.     charout (' ');
  3178. }
  3179.  
  3180.  
  3181. /* **************************************************************** */
  3182. /*                                    */
  3183. /*            Error output/handling.                */
  3184. /*                                    */
  3185. /* **************************************************************** */
  3186.  
  3187. /* Display specific error from known file error table. */
  3188. file_error (file)
  3189.      char *file;
  3190. {
  3191.   extern int errno;
  3192.   extern int sys_nerr;
  3193.   extern char *sys_errlist[];
  3194.  
  3195.   if (errno < sys_nerr)
  3196.     display_error ("%s: %s", file, sys_errlist[errno]);
  3197.   else
  3198.     display_error ("%s: Unknown error %d", file, errno);
  3199. }
  3200.  
  3201. /* Display the error in the echo-area using format_string and args.
  3202.    This is a specialized interface to printf. */
  3203. display_error (format_string, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
  3204.      char *format_string;
  3205. {
  3206.   extern boolean terminal_inited_p;
  3207.   char output_buffer[1024];
  3208.  
  3209.   if (totally_inhibit_errors)
  3210.     return;
  3211.  
  3212.   sprintf (output_buffer, format_string, arg1, arg2, arg3, arg4,
  3213.        arg5, arg6, arg7, arg8);
  3214.   if (terminal_inited_p)
  3215.     {
  3216.       new_echo_area ();
  3217.       ding ();
  3218.       print_string (output_buffer);
  3219.       close_echo_area ();
  3220.     }
  3221.   else
  3222.     {
  3223.       fprintf (stderr, "%s\n", output_buffer);
  3224.       sleep(1);
  3225.     }
  3226. }
  3227.  
  3228.  
  3229. /* Tell everybody what a loser I am.  If you see this error,
  3230.    send me a bug report. */
  3231. brians_error ()
  3232. {
  3233.   display_error ("You are never supposed to see this error.\n");
  3234.   display_error ("Tell bfox@ai.mit.edu to fix this someday.\n");
  3235.   return (-1);
  3236. }
  3237.  
  3238. /* **************************************************************** */
  3239. /*                                    */
  3240. /*            Terminal IO, and Driver                */
  3241. /*                                    */
  3242. /* **************************************************************** */
  3243.  
  3244. /* The Unix termcap interface code. */
  3245.  
  3246. #define NO_ERROR 0
  3247. #define GENERIC_ERROR 1
  3248. #define NO_TERMINAL_DESCRIPTOR 2
  3249. #define OUT_OF_MEMORY 3
  3250. #define BAD_TERMINAL 4
  3251.  
  3252. #define FERROR(msg)    fprintf (stderr, msg); exit (GENERIC_ERROR)
  3253.  
  3254. extern int tgetnum (), tgetflag ();
  3255. extern char *tgetstr ();
  3256. extern char *tgoto ();
  3257.  
  3258. #define Certainly_enough_space 2048    /* page 3, Section 1.1, para 4 */
  3259.  
  3260. #ifdef UNIX
  3261. char termcap_buffer[Certainly_enough_space];
  3262. #else
  3263. #define termcap_buffer NULL
  3264. #endif
  3265.  
  3266. /* You CANNOT remove these next four vars.  TERMCAP needs them to operate. */
  3267. char PC;
  3268. char *BC;
  3269. char *UP;
  3270.  
  3271. /* A huge array of stuff to get from termcap initialization. */
  3272.  
  3273. #define tc_int 0
  3274. #define tc_char tc_int+1
  3275. #define tc_flag tc_char+1
  3276. #define tc_last tc_flag+1
  3277.  
  3278. typedef int flag;
  3279.  
  3280. /* First, the variables which this array refers to */
  3281.  
  3282. /* Capabilities */
  3283.  
  3284. int terminal_columns;        /* {tc_int, "co" */
  3285. int terminal_rows;        /* {tc_int, "li" */
  3286. flag terminal_is_generic;    /* {tc_flag,"gn" */
  3287.  
  3288.  /* Cursor Motion */
  3289.  
  3290. char *terminal_goto;        /* {tc_char,"cm" */
  3291. char *terminal_home;        /* {tc_char,"ho" */
  3292.  
  3293. char *terminal_cursor_left;    /* {tc_char,"le" */
  3294. char *terminal_cursor_right;    /* {tc_char,"nd" */
  3295. char *terminal_cursor_up;    /* {tc_char,"up" */
  3296. char *terminal_cursor_down;    /* {tc_char,"do" */
  3297.  
  3298. /* Screen Clearing */
  3299.  
  3300. char *terminal_clearpage;    /* {tc_char,"cl" */
  3301. char *terminal_clearEOP;    /* {tc_char,"cd" */
  3302. char *terminal_clearEOL;    /* {tc_char,"ce" */
  3303.  
  3304. /* "Standout" */
  3305. char *terminal_standout_begin;    /* {tc_char,"so" */
  3306. char *terminal_standout_end;    /* {tc_char,"se" */
  3307.  
  3308. /* Reverse Video */
  3309. char *terminal_inverse_begin;    /* {tc_char,"mr" */
  3310. char *terminal_end_attributes;    /* {tc_char,"me" */
  3311.  
  3312. /* Ding! */
  3313.  
  3314. char *terminal_ear_bell;    /* {tc_char,"bl" */
  3315.  
  3316. /* Terminal Initialization */
  3317.  
  3318. char *terminal_use_begin;    /* {tc_char,"ti" */
  3319. char *terminal_use_end;        /* {tc_char,"te" */
  3320.  
  3321. /* Padding Stuff */
  3322.  
  3323. char *terminal_padding;        /* {tc_char,"pc" */
  3324.  
  3325. /* Now the whopping big array */
  3326.  
  3327. typedef struct {
  3328.   char type;
  3329.   char *name;
  3330.   char *value;
  3331. } termcap_capability_struct;
  3332.  
  3333. termcap_capability_struct capabilities[] = {
  3334.  
  3335. /* Capabilities */
  3336.   
  3337.   {tc_int, "co", (char *) &terminal_columns},
  3338.   {tc_int, "li", (char *) &terminal_rows},
  3339.   {tc_flag, "gn", (char *) &terminal_is_generic},
  3340.  
  3341. /* Cursor Motion */
  3342.  
  3343.   {tc_char, "cm", (char *) &terminal_goto},
  3344.   {tc_char, "ho", (char *) &terminal_home},
  3345.  
  3346.   {tc_char, "le", (char *) &terminal_cursor_left},
  3347.   {tc_char, "nd", (char *) &terminal_cursor_right},
  3348.   {tc_char, "up", (char *) &terminal_cursor_up},
  3349.   {tc_char, "do", (char *) &terminal_cursor_down},
  3350.  
  3351. /* Screen Clearing */
  3352.   
  3353.   {tc_char, "cl", (char *) &terminal_clearpage},
  3354.   {tc_char, "cd", (char *) &terminal_clearEOP},
  3355.   {tc_char, "ce", (char *) &terminal_clearEOL},
  3356.   
  3357. /* "Standout" */
  3358.   {tc_char, "so", (char *) &terminal_standout_begin},
  3359.   {tc_char, "se", (char *) &terminal_standout_end},
  3360.  
  3361. /* Reverse Video */
  3362.   {tc_char, "mr", (char *) &terminal_inverse_begin},
  3363.   {tc_char, "me", (char *) &terminal_end_attributes},
  3364.  
  3365. /* Ding! */
  3366.  
  3367.   {tc_char, "bl", (char *) &terminal_ear_bell},
  3368.   
  3369. /* Terminal Initialization */
  3370.  
  3371.   {tc_char, "ti", (char *) &terminal_use_begin},
  3372.   {tc_char, "te", (char *) &terminal_use_end},
  3373.  
  3374. /* Padding Stuff */
  3375.   
  3376.   {tc_char, "pc", (char *) &terminal_padding},
  3377.  
  3378. /* Terminate this array with a var of type tc_last */
  3379.   {tc_last, NULL, NULL}
  3380.  
  3381. };
  3382.  
  3383. int terminal_opened_p = 0;
  3384.  
  3385. open_terminal_io ()
  3386. {
  3387.   int error;
  3388.  
  3389.   if (terminal_opened_p)
  3390.     return (NO_ERROR);
  3391.  
  3392.   if ((error = get_terminal_info ()) != NO_ERROR)
  3393.     return (error);
  3394.  
  3395.   if ((error = get_terminal_vars (capabilities)) != NO_ERROR)
  3396.     return (error);
  3397.  
  3398.   /* Now, make sure we have the capabilites that we need. */
  3399.   if (terminal_is_generic)
  3400.     return (BAD_TERMINAL);
  3401.  
  3402.   terminal_opened_p++;
  3403.   return (NO_ERROR);
  3404. }
  3405.  
  3406. get_terminal_info ()
  3407. {
  3408.   char temp_string_buffer[256];
  3409.   int result;
  3410.  
  3411.   char *terminal_name = getenv ("TERM");
  3412.  
  3413.   if (terminal_name == NULL || *terminal_name == 0
  3414.       || (strcmp (terminal_name, "dialup") == 0))
  3415.     {
  3416.       terminal_name = temp_string_buffer;
  3417.       printf ("\nTerminal Type:");
  3418.       fflush (stdout);
  3419.       fgets (terminal_name, 256, stdin);
  3420.       if (!(*terminal_name))
  3421.     return (NO_TERMINAL_DESCRIPTOR);
  3422.     }
  3423.  
  3424. /* #define VERBOSE_GET_TERMINAL 1 */
  3425. #ifdef VERBOSE_GET_TERMINAL
  3426.  
  3427. #define buffer_limit 256
  3428. #define opsys_termcap_filename "/etc/termcap"
  3429.  
  3430.   /* We hack question mark if that is what the user typed.  All this means
  3431.      is we read /etc/termcap, and prettily print out the names of terminals
  3432.      that we find. */
  3433.  
  3434.   if (terminal_name[0] == '?' && !terminal_name[1])
  3435.     {
  3436.       FILE *termcap_file;
  3437.       if ((termcap_file = fopen (opsys_termcap_filename, "r")) != NULL)
  3438.     {
  3439.       int result;
  3440.       char line_buffer[buffer_limit];
  3441.       int terminal_count = 0;
  3442.  
  3443.       while ((readline_termcap (termcap_file, line_buffer)) != EOF)
  3444.         {
  3445.           char first_char = *line_buffer;
  3446.           if (first_char == '#' || first_char == ' '
  3447.           || first_char == '\t' || first_char == '\n')
  3448.         ;
  3449.           else
  3450.         {
  3451.           /* Print the names the pretty way. */
  3452.           printf ("\n%s", line_buffer);    /* liar */
  3453.           terminal_count++;
  3454.         }
  3455.         }
  3456.       fclose (termcap_file);
  3457.  
  3458.       if (terminal_count)
  3459.         printf ("\n%d terminals listed.\n", terminal_count);
  3460.       else
  3461.         printf ("\nNo terminals were listed.  Brian's mistake.\n");
  3462.     }
  3463.       else
  3464.     {
  3465.       fprintf (stderr,
  3466.            "\nNo such system file as %s!\nWe lose badly.\n",
  3467.            opsys_termcap_filename);
  3468.       return (NO_TERMINAL_DESCRIPTOR);
  3469.     }
  3470.       return (get_terminal_info ());
  3471.     }
  3472. #endif /* VERBOSE_GET_TERMINAL */
  3473.  
  3474.   result = tgetent (termcap_buffer, terminal_name);
  3475.  
  3476.   if (!result)
  3477.     return (NO_TERMINAL_DESCRIPTOR);
  3478.   else
  3479.     return (NO_ERROR);
  3480. }
  3481.  
  3482. #ifdef VERBOSE_GET_TERMINAL
  3483. readline_termcap (stream, buffer)
  3484.      FILE *stream;
  3485.      char *buffer;
  3486. {
  3487.   int c;
  3488.   int buffer_index = 0;
  3489.  
  3490.   while ((c = getc (stream)) != EOF && c != '\n')
  3491.     {
  3492.       if (buffer_index != buffer_limit - 1)
  3493.     buffer[buffer_index++] = c;
  3494.     }
  3495.  
  3496.   buffer[buffer_index] = 0;
  3497.  
  3498.   if (c == EOF)
  3499.     return ((buffer_index) ? 0 : EOF);
  3500.   else
  3501.     return (0);
  3502. }
  3503. #endif /* VERBOSE_GET_TERMINAL */
  3504.  
  3505. /* For each element of "from_array", read the corresponding variable's
  3506.    value into the right place. */
  3507. get_terminal_vars (from_array)
  3508.      termcap_capability_struct from_array[];
  3509. {
  3510.   int i;
  3511.   register termcap_capability_struct *item;
  3512.   char *buffer;
  3513.  
  3514. #if !defined (GNU_TERMCAP)
  3515.   buffer = (char *) xmalloc (sizeof (termcap_buffer) + 1);
  3516. # define buffer_space &buffer
  3517. #else
  3518. # define buffer_space 0
  3519. #endif
  3520.  
  3521.   for (i = 0; (item = &from_array[i]) && (item->type != tc_last); i++)
  3522.     {
  3523.       switch (item->type)
  3524.     {
  3525.     case tc_int:
  3526.       *((int *) (item->value)) = tgetnum (item->name);
  3527.       break;
  3528.  
  3529.     case tc_flag:
  3530.       *((int *) item->value) = tgetflag (item->name);
  3531.       break;
  3532.  
  3533.     case tc_char:
  3534.       *((char **) item->value) = tgetstr (item->name, buffer_space);
  3535.       break;
  3536.  
  3537.     default:
  3538.       FERROR ("Bad entry scanned in tc_struct[].\n \
  3539.            Ask bfox@ai.mit.edu to fix this someday.\n");
  3540.     }
  3541.     }
  3542.  
  3543.   PC = terminal_padding ? terminal_padding[0] : 0;
  3544.   BC = terminal_cursor_left;
  3545.   UP = terminal_cursor_up;
  3546.   return (NO_ERROR);
  3547. }
  3548.  
  3549. /* Return the number of rows this terminal has. */
  3550. int
  3551. get_terminal_rows ()
  3552. {
  3553.   int rows = 0;
  3554.  
  3555. #if defined (TIOCGWINSZ)
  3556.   {
  3557.     int tty;
  3558.     struct winsize size;
  3559.  
  3560.     tty = fileno (stdin);
  3561.  
  3562.     if (ioctl (tty, TIOCGWINSZ, &size) != -1)
  3563.       rows = size.ws_row;
  3564.   }
  3565. #endif /* TIOCGWINSZ */
  3566.  
  3567.   if (!rows)
  3568.     rows = tgetnum ("li");
  3569.  
  3570.   if (rows <= 0)
  3571.     rows = 24;
  3572.  
  3573.   return (rows);
  3574. }
  3575.  
  3576. /* Return the number of columns this terminal has. */
  3577. get_terminal_columns ()
  3578. {
  3579.   int columns = 0;
  3580.  
  3581. #if defined (TIOCGWINSZ)
  3582.   {
  3583.     int tty;
  3584.     struct winsize size;
  3585.  
  3586.     tty = fileno (stdin);
  3587.  
  3588.     if (ioctl (tty, TIOCGWINSZ, &size) != -1)
  3589.       columns = size.ws_col;
  3590.   }
  3591. #endif /* TIOCGWINSZ */
  3592.  
  3593.   if (!columns)
  3594.     columns = tgetnum ("co");
  3595.  
  3596.   if (columns <= 0)
  3597.     columns = 80;
  3598.  
  3599.   return (columns);
  3600. }
  3601.  
  3602. /* #define TERMINAL_INFO_PRINTING */
  3603. #ifdef TERMINAL_INFO_PRINTING
  3604.  
  3605. /* Scan this (already "get_terminal_vars"ed) array, printing out the
  3606.    capability name, and value for each entry.  Pretty print the value
  3607.    so that the terminal doesn't actually do anything, shitbrain. */
  3608. show_terminal_info (from_array)
  3609.      termcap_capability_struct from_array[];
  3610. {
  3611.   register int i;
  3612.   register termcap_capability_struct *item;
  3613.  
  3614.   for (i = 0; ((item = &from_array[i]) && ((item->type) != tc_last)); i++)
  3615.     {
  3616.  
  3617.       char *type_name;
  3618.       switch (item->type)
  3619.     {
  3620.     case tc_int:
  3621.       type_name = "int ";
  3622.       break;
  3623.     case tc_flag:
  3624.       type_name = "flag";
  3625.       break;
  3626.     case tc_char:
  3627.       type_name = "char";
  3628.       break;
  3629.     default:
  3630.       type_name = "Broken";
  3631.     }
  3632.  
  3633.       printf ("\t%s\t%s = ", type_name, item->name);
  3634.  
  3635.       switch (item->type)
  3636.     {
  3637.     case tc_int:
  3638.     case tc_flag:
  3639.       printf ("%d", *((int *) item->value));
  3640.       break;
  3641.     case tc_char:
  3642.       tc_pretty_print (*((char **) item->value));
  3643.       break;
  3644.     }
  3645.       printf ("\n");
  3646.     }
  3647. }
  3648.  
  3649. /* Print the contents of string without sending anything that isn't
  3650.    a normal printing ASCII character. */
  3651. tc_pretty_print (string)
  3652.      register char *string;
  3653. {
  3654.   register char c;
  3655.  
  3656.   while (c = *string++)
  3657.     {
  3658.       if (CTRLP (c))
  3659.     {
  3660.       putchar ('^');
  3661.       c += 64;
  3662.     }
  3663.       putchar (c);
  3664.     }
  3665. }
  3666. #endif TERMINAL_INFO_PRINTING
  3667.  
  3668.  
  3669. /* **************************************************************** */
  3670. /*                                    */
  3671. /*            Character IO, and driver            */
  3672. /*                                    */
  3673. /* **************************************************************** */
  3674.  
  3675. char *widest_line;
  3676. boolean terminal_inited_p = false;
  3677.  
  3678. /* Start up the character io stuff. */
  3679. init_terminal_io ()
  3680. {
  3681.   if (!terminal_inited_p)
  3682.     {
  3683.       opsys_init_terminal ();
  3684.       terminal_rows = get_terminal_rows ();
  3685.       terminal_columns = get_terminal_columns ();
  3686.  
  3687.       widest_line = (char *) xmalloc (terminal_columns);
  3688.  
  3689.       terminal_inited_p = true;
  3690.     }
  3691.  
  3692.   terminal_window.left = 0;
  3693.   terminal_window.top = 0;
  3694.   terminal_window.right = terminal_columns;
  3695.   terminal_window.bottom = terminal_rows;
  3696.  
  3697.   set_window (&terminal_window);
  3698.  
  3699.   terminal_window.bottom -= 2;
  3700.  
  3701.   set_window (&terminal_window);
  3702.  
  3703.   init_echo_area (the_window.left, the_window.bottom + 1,
  3704.           the_window.right, terminal_rows);
  3705.  
  3706.   /* Here is a list of things that the terminal has to be able to do. Do
  3707.      you think that this is too harsh? */
  3708.   if (!terminal_goto ||        /* We can't move the cursor. */
  3709.       !terminal_rows)        /* We don't how many lines it has. */
  3710.     {
  3711.       fprintf (stderr,
  3712.            "Your terminal is not clever enough to run info. Sorry.\n");
  3713.        exit (1);
  3714.     }
  3715. }
  3716.  
  3717. /* Ring the terminal bell. */
  3718. ding ()
  3719. {
  3720.   extern char *terminal_ear_bell;
  3721.  
  3722.   if (terminal_ear_bell)
  3723.     do_term (terminal_ear_bell);
  3724.   else
  3725.     putchar (CTRL ('G'));
  3726.  
  3727.   fflush (stdout);
  3728. }
  3729.  
  3730. int untyi_char = 0;
  3731. boolean inhibit_output = false;
  3732.  
  3733. /* Return a character from stdin, or the last unread character
  3734.    if there is one available. */
  3735. blink_cursor ()
  3736. {
  3737.   int character;
  3738.  
  3739.   fflush (stdout);
  3740.   if (untyi_char)
  3741.     {
  3742.       character = untyi_char;
  3743.       untyi_char = 0;
  3744.     }
  3745.   else
  3746.     do { character = getc (stdin); } while (character == -1 && errno == EINTR);
  3747.  
  3748.   return (character);
  3749. }
  3750.  
  3751. /* Display single character on the terminal screen.  If the
  3752.    character would run off the right hand edge of the screen,
  3753.    advance the cursor to the next line. */
  3754. charout (character)
  3755.      int character;
  3756. {
  3757.   if (inhibit_output)
  3758.     return;
  3759.  
  3760.   /* This character may need special treatment if it is
  3761.      a control character. */
  3762.   if (CTRL_P (character))
  3763.     {
  3764.       switch (character)
  3765.     {
  3766.     case NEWLINE:
  3767.     case RETURN:
  3768.       print_cr ();
  3769.       break;
  3770.  
  3771.     case TAB:
  3772.       print_tab ();
  3773.       break;
  3774.  
  3775.     default:
  3776.       charout ('^');
  3777.       charout (UNCTRL (character));
  3778.     }
  3779.     }
  3780.   else
  3781.     {
  3782.       putchar (character);
  3783.       advance (1);
  3784.     }
  3785. }
  3786.  
  3787. /* Move the cursor AMOUNT character positions. */
  3788. advance (amount)
  3789.      int amount;
  3790. {
  3791.   int old_window_cv = the_window.cv;
  3792.  
  3793.   while (amount-- > 0)
  3794.     {
  3795.       the_window.ch++;
  3796.       if (the_window.ch >= the_window.right)
  3797.     {
  3798.       the_window.ch = (the_window.ch - the_window.right) + the_window.left;
  3799.       the_window.cv++;
  3800.  
  3801.       if (the_window.cv >= the_window.bottom)
  3802.         the_window.cv = the_window.top;
  3803.     }
  3804.     }
  3805.  
  3806.   if (the_window.cv != old_window_cv)
  3807.     goto_xy (the_window.ch, the_window.cv);
  3808. }
  3809.  
  3810. /* Print STRING and args using charout */
  3811. print_string (string, a1, a2, a3, a4, a5)
  3812.      char *string;
  3813. {
  3814.   int character;
  3815.   char buffer[2048];
  3816.   int idx = 0;
  3817.  
  3818.   sprintf (buffer, string, a1, a2, a3, a4, a5);
  3819.  
  3820.   while (character = buffer[idx++])
  3821.     charout (character);
  3822.  
  3823.   fflush (stdout);
  3824. }
  3825.  
  3826. /* Display a carriage return.
  3827.    Clears to the end of the line first. */
  3828. print_cr ()
  3829. {
  3830.   extern boolean typing_out;
  3831.   clear_eol ();
  3832.  
  3833.   if (typing_out)
  3834.     {                /* Do the "MORE" stuff. */
  3835.       int response;
  3836.  
  3837.       if (the_window.cv + 2 == the_window.bottom)
  3838.     {
  3839.       goto_xy (the_window.left, the_window.cv + 1);
  3840.       clear_eol ();
  3841.       print_string ("[More]");
  3842.       response = blink_cursor ();
  3843.       if (response != SPACE)
  3844.         {
  3845.           untyi_char = response;
  3846.           inhibit_output = true;
  3847.           return;
  3848.         }
  3849.       else
  3850.         {
  3851.           goto_xy (the_window.left, the_window.cv);
  3852.           clear_eol ();
  3853.           goto_xy (the_window.left, the_window.top);
  3854.           return;
  3855.         }
  3856.     }
  3857.     }
  3858.   advance (the_window.right - the_window.ch);
  3859. }
  3860.  
  3861. /* Move the cursor to the next tab stop, blanking the intervening
  3862.    spaces along the way. */
  3863. print_tab ()
  3864. {
  3865.   int hpos, width, destination;
  3866.  
  3867.   hpos = the_window.ch - the_window.left;
  3868.   width = ((hpos + 8) & 0xf8) - hpos;
  3869.  
  3870.   destination = hpos + width + the_window.left;
  3871.  
  3872.   if (destination >= the_window.right)
  3873.     destination -= the_window.right;
  3874.  
  3875.   while (the_window.ch != destination)
  3876.     charout (SPACE);
  3877. }
  3878.  
  3879. display_width (character, hpos)
  3880.      int character, hpos;
  3881. {
  3882.   int width = 1;
  3883.  
  3884.   if (CTRL_P (character))
  3885.     {
  3886.       switch (character)
  3887.     {
  3888.     case RETURN:
  3889.     case NEWLINE:
  3890.       width = the_window.right - hpos;
  3891.       break;
  3892.     case TAB:
  3893.       width = ((hpos + 8) & 0xf8) - hpos;
  3894.       break;
  3895.     default:
  3896.       width = 2;
  3897.     }
  3898.     }
  3899.   return (width);
  3900. }
  3901.  
  3902. /* Like GOTO_XY, but do it right away. */
  3903. I_goto_xy (xpos, ypos)
  3904.      int xpos, ypos;
  3905. {
  3906.   goto_xy (xpos, ypos);
  3907.   fflush (stdout);
  3908. }
  3909.  
  3910. /* Move the cursor, (and cursor variables) to xpos, ypos. */
  3911. goto_xy (xpos, ypos)
  3912.      int xpos, ypos;
  3913. {
  3914.   the_window.ch = xpos;
  3915.   the_window.cv = ypos;
  3916.   opsys_goto_pos (xpos, ypos);
  3917. }
  3918.  
  3919. /* Clear the screen, leaving ch and cv at the top of the window. */
  3920. clear_screen ()
  3921. {
  3922.   goto_xy (the_window.left, the_window.top);
  3923.   clear_eop_slowly ();
  3924. }
  3925.  
  3926. clear_eop_slowly ()
  3927. {
  3928.   int temp_ch = the_window.ch;
  3929.   int temp_cv = the_window.cv;
  3930.  
  3931.   clear_eol ();
  3932.  
  3933.   while (++the_window.cv < the_window.bottom)
  3934.     {
  3935.       goto_xy (the_window.left, the_window.cv);
  3936.       clear_eol ();
  3937.     }
  3938.   goto_xy (temp_ch, temp_cv);
  3939. }
  3940.  
  3941. /* Clear from current cursor position to end of page. */
  3942. clear_eop ()
  3943. {
  3944.   if (terminal_clearEOP)
  3945.     do_term (terminal_clearEOP);
  3946.   else
  3947.     clear_eop_slowly ();
  3948. }
  3949.  
  3950. /* Clear from current cursor position to end of screen line */
  3951. clear_eol ()
  3952. {
  3953.   int temp_ch = the_window.ch;
  3954.  
  3955.   if (terminal_clearEOL)
  3956.     do_term (terminal_clearEOL);
  3957.   else
  3958.     {
  3959.       char *line = widest_line;
  3960.       int i;
  3961.  
  3962.       for (i = 0; i < the_window.right - the_window.ch; i++)
  3963.     line[i] = ' ';
  3964.       line[i] = '\0';
  3965.  
  3966.       printf ("%s", line);
  3967.     }
  3968.   goto_xy (temp_ch, the_window.cv);
  3969. }
  3970.  
  3971. /* Call FUNCTION with WINDOW active.  You can pass upto 5 args to the
  3972.    function.  This returns whatever FUNCTION returns. */
  3973. int
  3974. with_output_to_window (window, function, arg1, arg2, arg3, arg4, arg5)
  3975.      WINDOW *window;
  3976.      Function *function;
  3977. {
  3978.   int result;
  3979.  
  3980.   push_window ();
  3981.   set_window (window);
  3982.   result = (*function) (arg1, arg2, arg3, arg4, arg5);
  3983.   pop_window ();
  3984.   return (result);
  3985. }
  3986.  
  3987. /* Given a pointer to a window data structure, make that
  3988.    the current window. */
  3989. set_window (window)
  3990.      WINDOW *window;
  3991. {
  3992.   bcopy (window, &the_window, sizeof (WINDOW));
  3993. }
  3994.  
  3995. /* Save the current window on the window stack. */
  3996. push_window ()
  3997. {
  3998.   WINDOW_LIST *new_window = (WINDOW_LIST *) xmalloc (sizeof (WINDOW_LIST));
  3999.  
  4000.   new_window->next_window = window_stack;
  4001.   window_stack = new_window;
  4002.   new_window->ch = the_window.ch;
  4003.   new_window->cv = the_window.cv;
  4004.   new_window->top = the_window.top;
  4005.   new_window->bottom = the_window.bottom;
  4006.   new_window->left = the_window.left;
  4007.   new_window->right = the_window.right;
  4008. }
  4009.  
  4010. /* Pop the top of the window_stack into the_window. */
  4011. pop_window ()
  4012. {
  4013.   set_window ((WINDOW *)window_stack);
  4014.  
  4015.   if (window_stack->next_window)
  4016.     {
  4017.       WINDOW_LIST *thing_to_free = window_stack;
  4018.       window_stack = window_stack->next_window;
  4019.       free (thing_to_free);
  4020.     }
  4021.  
  4022.   goto_xy (the_window.ch, the_window.cv);
  4023. }
  4024.  
  4025. /* **************************************************************** */
  4026. /*                                    */
  4027. /*            "Opsys" functions.                */
  4028. /*                                    */
  4029. /* **************************************************************** */
  4030.  
  4031. /* The lowlevel terminal/file interface.  Nothing ever really gets
  4032.    low level when you're writing in C, though.
  4033.  
  4034.    This file contains all of the "opsys" labels.  You have to make
  4035.    a different one if you want GNU Info to run on machines that don't
  4036.    have Unix.  */
  4037.  
  4038. extern char *terminal_use_begin, *terminal_use_end, *terminal_goto;
  4039.  
  4040. #if defined (TIOCGETC)
  4041. struct tchars original_tchars;
  4042. #endif
  4043.  
  4044. #if defined (TIOCGLTC)
  4045. struct ltchars original_ltchars;
  4046. #endif
  4047.  
  4048. #if defined (USG)
  4049. struct termio original_termio, ttybuff;
  4050. #else
  4051. int original_tty_flags = 0;
  4052. int original_lmode;
  4053. struct sgttyb ttybuff;
  4054. #endif /* !USG */
  4055.  
  4056. /* Yes, that's right, do things that the machine needs to get
  4057.    the terminal into a usable mode. */
  4058. opsys_init_terminal ()
  4059. {
  4060.   int tty = fileno (stdin);
  4061.  
  4062. #if defined (USG)
  4063.   ioctl (tty, TCGETA, &original_termio);
  4064.   ioctl (tty, TCGETA, &ttybuff);
  4065.   ttybuff.c_iflag &= (~ISTRIP & ~INLCR & ~IGNCR & ~ICRNL &~IXON);
  4066.   ttybuff.c_oflag &= (~ONLCR & ~OCRNL);
  4067.   ttybuff.c_lflag &= (~ICANON & ~ECHO);
  4068.  
  4069.   ttybuff.c_cc[VMIN] = 1;
  4070.   ttybuff.c_cc[VTIME] = 0;
  4071.  
  4072.   if (ttybuff.c_cc[VINTR] = DELETE)
  4073.     ttybuff.c_cc[VINTR] = -1;
  4074.  
  4075.   if (ttybuff.c_cc[VQUIT] = DELETE)
  4076.     ttybuff.c_cc[VQUIT] = -1;
  4077.  
  4078.   ioctl (tty, TCSETA, &ttybuff);
  4079. #else /* !USG */
  4080.  
  4081.   ioctl (tty, TIOCGETP, &ttybuff);
  4082.  
  4083.   if (!original_tty_flags)
  4084.     original_tty_flags = ttybuff.sg_flags;
  4085.  
  4086.   /* Make this terminal pass 8 bits around while we are using it. */
  4087. #ifdef PASS8
  4088.   ttybuff.sg_flags |= PASS8;
  4089. #endif
  4090.  
  4091. #if defined (TIOCLGET) && defined (LPASS8)
  4092.   {
  4093.     int flags;
  4094.     ioctl (tty, TIOCLGET, &flags);
  4095.     original_lmode = flags;
  4096.     flags |= LPASS8;
  4097.     ioctl (tty, TIOCLSET, &flags);
  4098.   }
  4099. #endif
  4100.  
  4101. #ifdef TIOCGETC
  4102.   {
  4103.     struct tchars temp;
  4104.  
  4105.     ioctl (tty, TIOCGETC, &original_tchars);
  4106.     bcopy (&original_tchars, &temp, sizeof (struct tchars));
  4107.  
  4108.     temp.t_startc = temp.t_stopc = -1;
  4109.  
  4110.     /* If the quit character conflicts with one of our commands, then
  4111.        make it go away. */
  4112.     if (temp.t_intrc == DELETE)
  4113.       temp.t_intrc == -1;
  4114.  
  4115.     if (temp.t_quitc == DELETE)
  4116.       temp.t_quitc == -1;
  4117.  
  4118.     ioctl (tty, TIOCSETC, &temp);
  4119.   }
  4120. #endif /* TIOCGETC */
  4121.  
  4122. #ifdef TIOCGLTC
  4123.   {
  4124.     struct ltchars temp;
  4125.  
  4126.     ioctl (tty, TIOCGLTC, &original_ltchars);
  4127.     bcopy (&original_ltchars, &temp, sizeof (struct ltchars));
  4128.  
  4129.     /* Make the interrupt keys go away.  Just enough to make people happy. */
  4130.     temp.t_lnextc = -1;        /* C-v */
  4131.  
  4132.     ioctl (tty, TIOCSLTC, &temp);
  4133.   }
  4134. #endif /* TIOCGLTC */
  4135.  
  4136.   ttybuff.sg_flags &= ~ECHO;
  4137.   ttybuff.sg_flags |= CBREAK;
  4138.   ioctl (tty, TIOCSETN, &ttybuff);
  4139. #endif /* !USG */
  4140.  
  4141.   open_terminal_io ();
  4142.   do_term (terminal_use_begin);
  4143. }
  4144.  
  4145. /* Fix the terminal that I broke. */
  4146. restore_io ()
  4147. {
  4148.   int tty = fileno (stdin);
  4149.  
  4150. #if defined (USG)
  4151.   ioctl (tty, TCSETA, &original_termio);
  4152. #else
  4153.   ioctl (tty, TIOCGETP, &ttybuff);
  4154.   ttybuff.sg_flags = original_tty_flags;
  4155.   ioctl (tty, TIOCSETN, &ttybuff);
  4156.  
  4157. #ifdef TIOCGETC
  4158.   ioctl (tty, TIOCSETC, &original_tchars);
  4159. #endif /* TIOCGETC */
  4160.  
  4161. #ifdef TIOCGLTC
  4162.   ioctl (tty, TIOCSLTC, &original_ltchars);
  4163. #endif /* TIOCGLTC */
  4164.  
  4165. #if defined (TIOCLGET) && defined (LPASS8)
  4166.   ioctl (tty, TIOCLSET, &original_lmode);
  4167. #endif
  4168.  
  4169. #endif /* !USG */
  4170.   do_term (terminal_use_end);
  4171. }
  4172.  
  4173. opsys_goto_pos (xpos, ypos)
  4174.      int xpos, ypos;
  4175. {
  4176.   do_term (tgoto (terminal_goto, xpos, ypos));
  4177. }
  4178.  
  4179. character_output_function (character)
  4180.      char character;
  4181. {
  4182.   putchar (character);
  4183. }
  4184.  
  4185. /* Generic interface to tputs. */
  4186. do_term (command)
  4187.      char *command;
  4188. {
  4189.   /* Send command to the terminal, with appropriate padding. */
  4190.   tputs (command, 1, character_output_function);
  4191. }
  4192.  
  4193. /* Filename manipulators, and the like. */
  4194. char local_temp_filename[FILENAME_LEN];
  4195.  
  4196. char *info_suffixes[] = {
  4197.   "",
  4198.   ".info",
  4199.   "-info",
  4200.   (char *)NULL
  4201. };
  4202.  
  4203. /* Expand the filename in partial to make a real name for
  4204.    this operating system.  This looks in INFO_PATHS in order to
  4205.    find the correct file.  If it can't find the file, it just
  4206.    returns the path as you gave it. */
  4207. char *
  4208. opsys_filename (partial)
  4209.      char *partial;
  4210. {
  4211.   int initial_character;
  4212.  
  4213.   if (partial && (initial_character = *partial))
  4214.     {
  4215.  
  4216.       if (initial_character == '/')
  4217.     return (partial);
  4218.  
  4219.       if (initial_character == '~')
  4220.     {
  4221.       if (partial[1] == '/')
  4222.         {
  4223.           /* Return the concatenation of HOME and the rest
  4224.          of the string. */
  4225.           strcpy (local_temp_filename, getenv ("HOME"));
  4226.           strcat (local_temp_filename, &partial[2]);
  4227.           return (local_temp_filename);
  4228.         }
  4229.       else
  4230.         {
  4231.           struct passwd *user_entry;
  4232.           int i, c;
  4233.           char username[257];
  4234.  
  4235.           for (i = 1; c = partial[i]; i++)
  4236.         {
  4237.           if (c == '/')
  4238.             break;
  4239.           else
  4240.             username[i - 1] = c;
  4241.         }
  4242.           username[i - 1] = '\0';
  4243.  
  4244.           if (!(user_entry = getpwnam (username)))
  4245.         {
  4246.           display_error ("Not a registered user!");
  4247.           return (partial);
  4248.         }
  4249.           strcpy (local_temp_filename, user_entry->pw_dir);
  4250.           strcat (local_temp_filename, &partial[i]);
  4251.           return (local_temp_filename);
  4252.         }
  4253.     }
  4254.  
  4255.       if (initial_character == '.')
  4256.     {
  4257. #if defined (USG)
  4258.       if (!getcwd (local_temp_filename, FILENAME_LEN))
  4259. #else
  4260.       if (!getwd (local_temp_filename))
  4261. #endif
  4262.         {
  4263.           display_error (local_temp_filename);
  4264.           return (partial);
  4265.         }
  4266.  
  4267.       strcat (local_temp_filename, "/");
  4268.       strcat (local_temp_filename, partial);
  4269.       return (local_temp_filename);
  4270.     }
  4271.  
  4272.       /* Scan the list of directories in INFOPATH. */
  4273.       {
  4274.     struct stat finfo;
  4275.     char *temp_dirname, *extract_colon_unit ();
  4276.     int statable, dirname_index = 0;
  4277.  
  4278.     while (temp_dirname = extract_colon_unit (infopath, &dirname_index))
  4279.       {
  4280.         register int i;
  4281.  
  4282.         for (i = 0; info_suffixes[i]; i++)
  4283.           {
  4284.         strcpy (local_temp_filename, temp_dirname);
  4285.  
  4286.         if (temp_dirname[(strlen (temp_dirname)) - 1] != '/')
  4287.           strcat (local_temp_filename, "/");
  4288.  
  4289.         strcat (local_temp_filename, partial);
  4290.         strcat (local_temp_filename, info_suffixes[i]);
  4291.         
  4292.         statable = (stat (local_temp_filename, &finfo) == 0);
  4293.  
  4294.         if (statable && (finfo.st_mode & S_IFMT) == S_IFREG)
  4295.           {
  4296.             free (temp_dirname);
  4297.             return (local_temp_filename);
  4298.           }
  4299.           }
  4300.         free (temp_dirname);
  4301.       }
  4302.       }
  4303.     }
  4304.   return (partial);
  4305. }
  4306.  
  4307. /* Given a string containing units of information separated by colons,
  4308.    return the next one pointed to by IDX, or NULL if there are no more.
  4309.    Advance IDX to the character after the colon. */
  4310. char *
  4311. extract_colon_unit (string, idx)
  4312.      char *string;
  4313.      int *idx;
  4314. {
  4315.   register int i, start;
  4316.  
  4317.   i = start = *idx;
  4318.   if ((i >= strlen (string)) || !string)
  4319.     return ((char *) NULL);
  4320.  
  4321.   while (string[i] && string[i] != ':')
  4322.     i++;
  4323.   if (i == start)
  4324.     {
  4325.       return ((char *) NULL);
  4326.     }
  4327.   else
  4328.     {
  4329.       char *value = xmalloc (1 + (i - start));
  4330.       strncpy (value, &string[start], (i - start));
  4331.       value[i - start] = '\0';
  4332.       if (string[i])
  4333.     ++i;
  4334.       *idx = i;
  4335.       return (value);
  4336.     }
  4337. }
  4338.  
  4339. /* **************************************************************** */
  4340. /*                                    */
  4341. /*            The echo area.                    */
  4342. /*                                    */
  4343. /* **************************************************************** */
  4344.  
  4345. /* echoarea.c -- some functions to aid in user interaction. */
  4346.  
  4347. WINDOW echo_area = {0, 0, 0, 0, 0, 0};
  4348. boolean echo_area_open_p = false;
  4349. char modeline[256];
  4350. WINDOW modeline_window = {0, 0, 0, 0, 0, 0};
  4351.  
  4352. /* Define the location of the echo area. Also inits the
  4353.    modeline as well. */
  4354. init_echo_area (left, top, right, bottom)
  4355.      int left, top, right, bottom;
  4356. {
  4357.   echo_area.left = modeline_window.left = left;
  4358.   echo_area.top = top;
  4359.   modeline_window.top = top - 1;
  4360.   echo_area.right = modeline_window.right = right;
  4361.   echo_area.bottom = bottom;
  4362.   modeline_window.bottom = modeline_window.top;
  4363. }
  4364.  
  4365. /* Make the echo_area_window be the current window, and only allow
  4366.    output in there.  Clear the window to start. */
  4367. new_echo_area ()
  4368. {
  4369.   if (!echo_area_open_p)
  4370.     {
  4371.       push_window ();
  4372.       set_window (&echo_area);
  4373.       echo_area_open_p = true;
  4374.     }
  4375.   goto_xy (the_window.left, the_window.top);
  4376.   clear_eop ();
  4377. }
  4378.  
  4379. /* Return output to the previous window. */
  4380. close_echo_area ()
  4381. {
  4382.   if (!echo_area_open_p)
  4383.     return;
  4384.  
  4385.   pop_window ();
  4386.   echo_area_open_p = false;
  4387. }
  4388.  
  4389. /* Clear the contents of the echo area. */
  4390. clear_echo_area ()
  4391. {
  4392.   new_echo_area ();
  4393.   close_echo_area ();
  4394. }
  4395.  
  4396. /* Create and display the modeline. */
  4397. make_modeline ()
  4398. {
  4399.   extern int info_buffer_len;
  4400.   int width = modeline_window.right - modeline_window.left;
  4401.  
  4402.   sprintf (modeline, "Info: (%s)%s, %d lines",
  4403.        current_info_file, current_info_node, nodelines);
  4404.   if (strnicmp
  4405.       (opsys_filename (current_info_file), last_loaded_info_file,
  4406.        strlen (last_loaded_info_file)) != 0)
  4407.     {
  4408.       sprintf (&modeline[strlen (modeline)], ", Subfile: %s",
  4409.            last_loaded_info_file);
  4410.     }
  4411.  
  4412.   if (strlen (modeline) < width)
  4413.     {
  4414.       int idx = strlen (modeline);
  4415.       while (idx != width)
  4416.     modeline[idx++] = '-';
  4417.       modeline[idx] = '\0';
  4418.     }
  4419.  
  4420.   if (strlen (modeline) > width)
  4421.     modeline[width] = '\0';
  4422.   push_window ();
  4423.   set_window (&modeline_window);
  4424.   goto_xy (the_window.left, the_window.top);
  4425.  
  4426.   if (terminal_inverse_begin)
  4427.     do_term (terminal_inverse_begin);
  4428.   print_string (modeline);
  4429.   if (terminal_inverse_begin)
  4430.     do_term (terminal_end_attributes);
  4431.  
  4432.   pop_window ();
  4433. }
  4434.  
  4435. boolean typing_out = false;
  4436.  
  4437. /* Prepare to do output to the typeout window.  If the
  4438.    typeout window is already open, ignore this clown. */
  4439. open_typeout ()
  4440. {
  4441.   if (typing_out)
  4442.     return;
  4443.  
  4444.   push_window ();
  4445.   set_window (&terminal_window);
  4446.   goto_xy (the_window.ch, the_window.cv);
  4447.   typing_out = window_bashed = true;
  4448. }
  4449.  
  4450. /* Close the currently open typeout window. */
  4451. close_typeout ()
  4452. {
  4453.   if (inhibit_output)
  4454.     inhibit_output = false;
  4455.   else
  4456.     {
  4457.       do { untyi_char = getc (stdin); }
  4458.         while (untyi_char == -1 && errno == EINTR);
  4459.  
  4460.       if (untyi_char == SPACE)
  4461.     untyi_char = 0;
  4462.     }
  4463.   pop_window ();
  4464.   typing_out = false;
  4465. }
  4466.  
  4467. char *
  4468. xrealloc (pointer, bytes)
  4469.      char *pointer;
  4470.      int bytes;
  4471. {
  4472.   char *temp;
  4473.  
  4474.   if (!pointer)
  4475.     temp = (char *)xmalloc (bytes);
  4476.   else
  4477.     temp = (char *)realloc (pointer, bytes);
  4478.  
  4479.   if (!temp)
  4480.     {
  4481.       fprintf (stderr, "Virtual memory exhausted\n");
  4482.       restore_io ();
  4483.       exit (2);
  4484.     }
  4485.   return (temp);
  4486. }
  4487.  
  4488. char *
  4489. xmalloc (bytes)
  4490.      int bytes;
  4491. {
  4492.   char *temp = (char *) malloc (bytes);
  4493.  
  4494.   if (!temp)
  4495.     {
  4496.       fprintf (stderr, "Virtual memory exhausted\n");
  4497.       restore_io ();
  4498.       exit (2);
  4499.     }
  4500.   return (temp);
  4501. }
  4502.